CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2009
    Posts
    16

    String2DataType function

    Hi,

    I want to implement a function that take a given a string and return, if it exists, a data type so it can be used for casting later. Here's an example of what I'd like to accomplish:

    (String2DataType("System.Windows.Forms.Form"))object;
    obj.Show();

    Is there such a function in c# or at least a way to implement that?

    Regards.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: String2DataType function

    Casting is a compile time process, not runtime. If you need to dynamically create an object of an unknown type at runtime you will need to look into reflection and you will also need an interface so that you can use the object in your code.

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: String2DataType function

    I have no idea what you are trying to achieve.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #4
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: String2DataType function

    I think what you need is to Use Activator.CreateInstance to create an object from a string:
    Check: http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  5. #5
    Join Date
    Dec 2009
    Posts
    22

    Re: String2DataType function

    So you have the name of the type and you want to cast and object to that type.

    Somebody have had a similar problem over at stack overflow check it out, it might help you:
    http://stackoverflow.com/questions/7...representation

  6. #6
    Join Date
    Aug 2009
    Posts
    16

    Re: String2DataType function

    Thank you all for your replies. I changed the implementation so I won't require to do it, it was a poor design that has thrown me to the place I needed to do that.
    I was sending serialized objects through the network. I wanted to add a header string in each message indicating the object being serialized so the other side can cast it in the right way.
    I'm not sending serialized objects anymore, messages are embedded within a xml structure.
    Thank you all, again. I really appreciate it.

    Best regards.

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

Featured