Tuesday, October 8, 2013

Reflection C# .NET

Reflection:

If we are working on many project in one solution of visual studio and need to find the type of anything, e.g. Enum. We can use the following code to find from current executing assembly from current assembly references.
private static Type GetEnumTypeFromAssembly(String enumTypeName)
{
     var currentAssembly = Assembly.GetExecutingAssembly();
     Type type = null;
     foreach (var assemblyname in 
                           currentAssembly.GetReferencedAssemblies())
     {
           type = Assembly.Load(assemblyname).GetType((enumTypeName));
           if (type != null)
           {
                break;
           }
     }
     return type;
}