Edit: I am using DataSourceProviderService.InvokeAddNewDataSource
method to display Data Source Configuration Wizard during design-time in Visual Studio. If user choose an object (as explained here), and click finish, I will get a string like "Namespace.ClassName"
. To display the Properties of the selected object in designer, I need to find the correct Type
of the object in an optimized manner.
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;
}
}
Note: Assembly might be present in any dll referenced in the project. My code also supports .Net Framework 2.0
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
3) On debugging found that Types in unwanted or unnecessary assemblies are checked in the loop. AppDomain.CurrentDomain.GetAssemblies()
method returns 146 assemblies in a simple project during design-time debugging
4) If above code loads an unnecessary assembly into memory, it will present in memory till application domain is present (check unloading an assembly section in this link https://msdn.microsoft.com/en-us/library/mt632258.aspx)
Is there any recommended way or best approach for doing the same?