Detect Type with Reflection?

  • Thread starter Thread starter xenophon
  • Start date Start date
X

xenophon

I have an instanced class that needs to, at Runtime using Reflection,
check the current AppDomain for the existence of another Type, and if
it exists create a new instance. If that Type is not in the AppDomain,
then attempt to load it from disk.

How can I do that (C#)?

Thanks.
 
I have an instanced class that needs to, at Runtime using Reflection,
check the current AppDomain for the existence of another Type

You can iterate through the assemblies returned by
AppDomain.GetAssemblies and check for the one where teh type is
implemented.
and if it exists create a new instance.

When you find the right Assembly instance just call CreateInstance on
it and pass in the type name.

If that Type is not in the AppDomain,
then attempt to load it from disk.

With Assembly.Load/LoadFrom. You can always do this if you want (skip
checking if it's already loaded) since the assembly will only loaded
once anyway.



Mattias
 
Back
Top