Converting type definition type strings from C # style to CLR style

I want to convert a general C # string style type , for example:

"System.Dictionary<System.String, System.String>"

To its CLR equivalent:

"System.Dictionary`1[System.String, System.String]"

and back. Is there an easy way to do this, or do I need to resort to string manipulations?

EDIT:

I only have a string representation in the form of a C # / VB / etc style. I don't have an object like or something like that. An alternative question: how to get a Type object from a string representation, for example "System.Dictionary<System.String, System.String>"(after that I can get the full name of the type object).

EDIT # 2:

: , CodeDom. , CodeDom, C# ( , VB.NET) , CodeTypeReference, CLR.

+3
6

, :

    // Utilitiy to convert class name string to namespace-qualified name
    public static Type GetTypeByName(string ClassName, 
        string TypesNamespacePrefix = "AssemblyTopLevel.AssemblyMidLevel.", 
        Dictionary<string, Type> ConcreteTypeMap = null)
    {
        if ((ConcreteTypeMap != null) && (ConcreteTypeMap.ContainsKey(ClassName)))
            return ConcreteTypeMap[ClassName];

        if ((ConcreteTypeMap != null) && (ConcreteTypeMap.ContainsKey(TypesNamespacePrefix + ClassName)))
            return ConcreteTypeMap[TypesNamespacePrefix + ClassName];

        try
        {
            if (Type.GetType(ClassName) != null)
                return Type.GetType(ClassName);
        }
        catch { }

        try
        {
            if (Type.GetType(TypesNamespacePrefix + ClassName) != null)
                return Type.GetType(TypesNamespacePrefix + ClassName);
        }
        catch { }

        Stack<int> GenericCounterStack = new Stack<int>();
        Stack<int> GenericStartIndexStack = new Stack<int>();
        Dictionary<int, int> GenericCountMapByStartIndex = new Dictionary<int, int>();
        int Count = 1;
        int GenericStartIndex = -1;
        int PreviousHighestGenericIndex = -1;

        foreach (char c in ClassName)
        {
            if (c.Equals('<'))
            {
                if (GenericStartIndex != -1)
                {
                    GenericCounterStack.Push(Count);
                    GenericStartIndexStack.Push(GenericStartIndex);
                }
                Count = 1;
                GenericStartIndex = PreviousHighestGenericIndex + 1;
                PreviousHighestGenericIndex = Math.Max(GenericStartIndex, PreviousHighestGenericIndex);
            }
            else if (c.Equals(','))
            {
                Count++;
            }
            else if (c.Equals('>'))
            {
                GenericCountMapByStartIndex[GenericStartIndex] = Count;
                if (GenericCounterStack.Count != 0)
                    Count = GenericCounterStack.Pop();
                if (GenericStartIndexStack.Count != 0)
                    GenericStartIndex = GenericStartIndexStack.Pop();
            }
        }

        ClassName = ClassName.Replace("<" + TypesNamespacePrefix, "<");

        StringBuilder FullyQualifiedClassName = new StringBuilder(TypesNamespacePrefix);

        GenericStartIndex = 0;
        foreach (char c in ClassName)
        {
            if (c.Equals('<'))
            {
                FullyQualifiedClassName.Append("`" + GenericCountMapByStartIndex[GenericStartIndex].ToString()
                    + "[" + TypesNamespacePrefix);
                GenericStartIndex++;
            }
            else if (c.Equals(','))
            {
                FullyQualifiedClassName.Append("," + TypesNamespacePrefix);
            }
            else if (c.Equals('>'))
            {
                FullyQualifiedClassName.Append("]");
            }
            else
                FullyQualifiedClassName.Append(c);
        }

        ClassName = FullyQualifiedClassName.ToString();

        return Type.GetType(ClassName);
    }

, :

static readonly Dictionary <string, Type> MyConcreteTypeMap = new Dictionary<string,Type>()
    {
        {"SomeClass", typeof(SomeClass)},
        {"AnotherClass", typeof(AnotherClass)}
    }

, :

Type DesiredType = GetTypeByName("SomeClass", "", MyConcreteTypeMap);

Type DesiredType = GetTypeByName("SomeOtherClass", "MyAssemblyTopLevel.MyAssemblyMidLevel.");

Type DesiredType = GetTypeByName("SomeOtherClass");

, ,

Type DesiredType = GetTypeByName("SomeOtherClass<OuterTemplate<InnerTemplate1,InnerTemplate2<InnerInnerTemplate1>>>");

, ,

Type DesiredType = GetTypeByName("Dictionary<String,String>", "System.");
+2

.NET # ; , , .

:

public static string DotNetToCSharp(string tyName) {
    var provider = new Microsoft.CSharp.CSharpCodeProvider();
    return provider.GetTypeOutput(new System.CodeDom.CodeTypeReference(tyName));
}
+4
typeof(System.Dictionary<System.String>, System.String>).FullName

, : (

0

, . - Class<T1, ... ,Tn>, ,

Namespace.Class<T1, ... ,Tn> a = new Namespace.Class<Type1,...,Typen>();

a.GetType(). FullName "Namespace.Class'n<Type1,...,Typen>"

0

typeof(System.Dictionary<System.String, System.String>).FullName - :

System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

, . ,

typeof(Dictionary<System.String, System.String>).ToString()

- :

System.Collections.Generic.Dictionary`2[System.String,System.String]

Type

Type.GetType("System.Collections.Generic.Dictionary`2[System.String,System.String]");

EDIT I tried to hack this with dynamic expressions, but didn't have much luck. I think, in addition to parsing the string, you can use the "kernel option" and compile the DLL using CSharpCodeProvider, and then load it, using Assembly.LoadFileand execute a method that will evaluate typeof (...).

0
source

I think this is what you were looking for ...

var dict = new Dictionary<string, string>();
var type = dict.GetType();
var typeName = type.FullName;
var newType = Type.GetType(typeName);

Console.WriteLine(type == newType); //true
0
source

Source: https://habr.com/ru/post/1747452/


All Articles