views:

44

answers:

2

I need a function that will convert a type to a string, for example:

Dim foo as Dictionary(of Dictionary(of Integer, String), Integer)
Debug.WriteLine(TypeToString(GetType(foo)))

In the debug output, I'd expect to see something equivalent to:

Dictionary(of Dictionary(of Integer, String), Integer)
A: 

Isn't foo.GetType().ToString() acceptable for you? In the example case it will return

System.Collections.Generic.Dictionary`2[System.Collections.Generic.Dictionary`2[System.Int32,System.String],System.Int32]
wRAR
I can't compile that.
Eyal
+1  A: 

Here's some extension methods I use to do what you ask, but it's in C# and generate C#.

You can either keep it in C#, but change the output to VB.Net or do a full conversion to VB.Net if you like.

public static string ToCode(this Type @this)
{
    string @return = @this.FullName ?? @this.Name;
    Type nt = Nullable.GetUnderlyingType(@this);
    if (nt != null)
    {
        return string.Format("{0}?", nt.ToCode());
    }
    if (@this.IsGenericType & [email protected])
    {
        Type gtd = @this.GetGenericTypeDefinition();
        return string.Format("{0}<{1}>", gtd.ToCode(), @this.GetGenericArguments().ToCode());
    }
    if (@return.EndsWith("&"))
    {
        return Type.GetType(@this.AssemblyQualifiedName.Replace("&", "")).ToCode();
    }
    if (@this.IsGenericTypeDefinition)
    {
        @return = @return.Substring(0, @return.IndexOf("`"));
    }
    switch (@return)
    {
        case "System.Void":
            @return = "void";
            break;

        case "System.Int32":
            @return = "int";
            break;

        case "System.String":
            @return = "string";
            break;

        case "System.Object":
            @return = "object";
            break;

        case "System.Double":
            @return = "double";
            break;

        case "System.Int64":
            @return = "long";
            break;

        case "System.Decimal":
            @return = "decimal";
            break;

        case "System.Boolean":
            @return = "bool";
            break;
    }
     return @return;
}

public static string ToCode(this IEnumerable<Type> @this)
{
    var @return = "";
    var ts = @this.ToArray<Type>();
    if (ts.Length > 0)
    {
        @return = ts[0].ToCode();
        if (ts.Length > 1)
        {
            foreach (Type t in ts.Skip<Type>(1))
            {
                @return = @return + string.Format(", {0}", t.ToCode());
            }
        }
    }
    return @return;
}
Enigmativity
Naming variables `return` and `this` is a horrible idea.
Darin Dimitrov
I think it is fabulous. It is very clear what variable is being extended by the extension method and I can clearly see what value I'm returning. The "@" helps highlight these two important variables. Darin, can you tell me why you think it is a horrible idea?
Enigmativity
@Enigmativity: I think I agree with @Darin here. The words `this` and `return` have very specific meanings and usages. Even though you (by necessity) prefix the variables with `@`, I still find it more confusing than clarifying. Instead of `@return` I would personally name the variable `result`. `return result;` seems more obvious than `return @return;` In the case of `@this` it could simply be named `type` or `typeSequence`, which I think better helps to clarify what it represents.
Fredrik Mörk
Well, it fits the poster's nick. But yeah, the code looks like a database for a spammer. Best avoided in a Q+A site.
Hans Passant