简体   繁体   中英

How can I obtain the System.Type of a type in an external shared assembly?

I created a shared assembly MyTypes.dll that contains a public FooBar class type. I have referenced it in multiple projects and it works as expected.

I have determined its full display name to be: "MyTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=624e4a808e6fc010" .

I created a new project NewProject and added MyTypes as a reference. I would like to determine the members of FooBar from within NewProject using System.Type.GetType() .

Here is what I've written:

// NewProject.cs
using System;
using System.Reflection;

// namespace... class... method...
Type fb;
fb = Type.GetType("MyTypes.FooBar, MyTypes"); // or the full display name
// Exception will be thrown because fb is set to null
MemberInfo[] fbMembers = fb.GetMembers();

However, as noted in the code comments, I cannot provide the correct string format to GetType in order to return a valid Type for the external assembly. Also, this approach will work for anything within the current assembly.

EDIT Turns out the public key token was missing a digit (copy/paste).

The Type.GetType(string) method requires the assembly qualified type name which looks like this:

<Namespace>.<TypeName>, <AssemblyName>, Version=XXXX, Culture=neutral, PublicKeyToken=null

However, if you've referenced the assembly, you should just be able to use typeof(FooBar).GetMembers() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM