Sign in to follow this  
fanaticlatic

[.net] Retrieving Type objects

Recommended Posts

Hi, I am trying to create a delegate using delegate.CreateDelegate(typeof(delegateObj), (Type)scriptType, (string)methodname) where scriptType is built using scriptType = Type.GetType(asmQualifiedName,true,true); this way of delegate creation currently forces me to store the whole assembly qualified name in my script file for the class that holds the method 'methodname' which as you probably know is quite a hefty string: "someProject.SomeObjType, someProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" ideally i would just like to store the strings "someProject.SomeObjType" or even better "SomeObjType" then build the Type object 'scriptType' from that. If anyone knows a method that will take a fullname or name of a class rather than the assembley qualified name i would be interested to know. i should point out that i thought about creating a constant string with the back end of that assembley name but that would restrict which classes delegates could be created from. thanks in advance, mark.

Share this post


Link to post
Share on other sites
I'm not sure I totally understand your question. But if you want a more flexible type locating system, you can write one yourself easily. Look at the Assembly class. You can do

Assembly a = Assembly.GetExecutingAssembly();

Type[] types = a.GetTypes();


to get a list of all types in the assembly which contains the code that's currently executing. Then you can iterate through each type and compare it to your strings. You can also take an assembly and search through all its referenced assemblies using a.GetReferencedAssemblies() to get an array of assemblies that are referenced, and you can search through each of the types in those assemblies.

You can go further than that, and do AppDomain.GetAssemblies() which returns all loaded assemblies, including any assemblies that you have loaded through reflection at runtime.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this