English 中文(简体)
在对象列表中装入未引用的组装类型
原标题:Load the type of an unreferenced assembly within an object list

我想从一个未引用的组装中获得一种类型 。 我用< a href="https://stackoverflow.com/a/ 463366/1384848> >这个答案 来解决我的问题 。

现在的问题是, 我装入类型 < code> ObjectList , 它包含来自其它组装的另一种类型 。 我喜欢这样:

Assembly assembly = Assembly.LoadFrom ("@c:myAssembliesmyAssembly.Data.DomainObjects.dll");
Type myType = assembly.GetType ("myAssembly.Data.DomainObjects.ObjectList`1[[myAssembly.otherNamespace.myClass, myAssembly.otherNamespace, Version=1.13.73.1082, Culture=neutral, PublicKeyToken=fee00910d6e5f53b]]");

带有 otherNamespace 的组装也没有被引用,因此, GetType 方法返回 null 。 现在我的问题是:

是否有可能获得包含其它类型未引用组装的对象列表类型? 或者 : 我如何在我的 < code> ObjectList 内装入类型组装?

最佳回答

这对我来说有点难测试 但我希望我的答案能转换为:

基本上,你需要分两步走

  1. resolve the generic parameter type (myAssembly.otherNamespace.myClass, in your case)
  2. generate the new type, based on type of the generic parameter.

这里的 Linqpad 脚本 :

void Main()
{
    //resolve the inner type
    var  parameterType = Type.GetType("UserQuery+GenericParameter");

    //Use the generic type, and generate with the inner parameter type
    var genericTypeWithParameter = Type.GetType("UserQuery+Moop`1")
        .MakeGenericType(parameterType);

    //The resolved type - printed to console
    genericTypeWithParameter.Dump();
}

public class Moop <T> { }
public class GenericParameter { }
问题回答

我不百分百确定,但当我尝试这个:

public class Moop<T> {}

我这样做:

Type.GetType("UserQuery.Moop`1") //returns null

Type.GetType("UserQuery+Moop`1") //returns the correct type

林巴语Name

+意味着它只是一个嵌套类型 - 所以,这也许不是正确的答案。

如果组装 my Assembly. otherNamespace.dll 不在与可执行文件或安装在 GAC 相同的目录中, 通用语言运行时间无法定位它。 因此, my Assembly. dll 没有装入 AppDomain, 您无法获得您想要的类型 。

myAssembly.Data.DomainObjects myAssembly.otherNamespace.dll 放在执行文件或安装文件到 GAC的同一目录上。 然后使用 < href="http://msdn.microft.com/ en-us/library/ky3942xh.aspx" rel="nofollow" > (而不是





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...