Results 1 to 3 of 3

Thread: [3.0/LINQ] Get Type object from string

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    [3.0/LINQ] Get Type object from string

    I'm reading a string from a file, which would be something like "char", or "bool" etc. I'm trying to get a type object from it. While working out how to do this i've tried the code below, but it doesn't work.

    Any ideas why?
    Code:
    Type T = Assembly.GetAssembly(typeof(char)).GetType("System.char", true);
    Type T = Assembly.GetAssembly(typeof(char)).GetType("char", true);
    Thanks for any help.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [3.0/LINQ] Get Type object from string

    This works fine for me:
    CSharp Code:
    1. Assembly asm = Assembly.GetAssembly(typeof(char));
    2. Type typ = asm.GetType("System.Char");
    That said, it's useless for you because you're using the actual type to get the assembly in the first place, but you don't know the actual type at design time because you're not reading the type name until run time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: [3.0/LINQ] Get Type object from string

    Thanks for the reply. I've got it working now, not sure why my testing example didn't work.

    What i was trying was just an example while i was figuring out relevent classes etc. then i encountered the error and thought i was doing something fundamentally wrong. I realise i can't do typeof(char) since i dont' know what 'char' will be.

    Thanks again.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width