mardi 14 juin 2016

How to find Type of a class with namespace name and class name?

I have the name of a class and its namespace (Application.Data.Employee). I want to find the type of the class (Employee) with this information. At present I am using the following code to find the type

string classNameWithNameSpace = "Application.Data.Employee";
Type target;                                
foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
    foreach (Type t in assembly.GetTypes())
    {
        if (t.FullName.Equals(classNameWithNameSpace))
        {
            target = t;
            break;
        }
    }

I know this is not the best way because of the following reasons

1) Two or more assemblies might have same namespace name and class name

2) I saw a SO post stating, it will throw NotSupportedException for dynamic assemblies

Is there any recommended way or best approach for doing the same?





Aucun commentaire:

Enregistrer un commentaire