Determine the actual datatype stored in a string variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sri13
    New Member
    • Sep 2010
    • 11

    Determine the actual datatype stored in a string variable

    I have a string variable that stores the datatype name. Datatype name can be any datatype from native to user defined.
    Examples:
    string sType = "System.Boolean ";
    string sType = "System.Drawing .Color";
    string sType = "MyNamespace.My Class.Helper";

    Is there any easier (inbult) way to check if sType is a native type or a user defined type. And also, can I get the list of all the possible values of the datatype stored in sType? Example: Boolean - true, false and Color should return all the system colors.

    or, do I need to write code like:
    Code:
    if (sType == "System.Boolean")
    {
      //do something
    }
    else if (Type == "System.Drawing.Color")
    {
      //do something, but how to get the list of all the colors?
    }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I don't know of a way to get a list of all the built in types... it's kind of tricky since they go across namespaces and I think anybody could create a namespace that was something like System.GaryTexm o.MyNamespace so you couldn't just check for a System prefix.

    As for getting a list of colours, that I can help you with. You can use the GetProperties method on the colour type. Here's a code example.

    Code:
                PropertyInfo[]  piArray = colorType.GetProperties(BindingFlags.Static | BindingFlags.Public);
                foreach (PropertyInfo pi in piArray)
                    Console.WriteLine(pi.Name);
    For the Color object, it just so happens that the colours are all static properties. So you can just get the list of those.

    An alternative is to use the Enum.GetNames on the KnownColor enumeration.

    Comment

    • Sri13
      New Member
      • Sep 2010
      • 11

      #3
      Thanks Gary for the response. I don't want to get the listo f built in datatypes. I want the list of possible values of those types. If I pass System.Boolean. Then it should return the possible values as "True" and "False". For Int, string etc it will be default values of 0, null etc. Also, I am not sure how to convert the datatype that is stored in a string variable to Type?

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Oh, I see. Well the second is easy... Look into Type.GetType(.. .) and that should help you out.

        For the first, I don't know if you can do that. There might be a way, I just don't know it. That said, there's only so many primitive types in C#, you can just switch on that. For everything else, you can set it to null. Well, not quite... you can't set a struct to null unless you define it as a nullable type.

        I'm not sure how you'd do that with reflection though.

        Comment

        Working...