English 中文(简体)
如何在上诉书中以建筑论提出反对? (NET 3.5)
原标题:How to create an object with constructor arguments in an AppDomain? (.NET 3.5)

I am trying to create an instance of an object inside of an AppDomain.
With the below code, I get an Exception "Type is not resolved for member"

该守则是:

private  T GetInstance<T>(AppDomain domain, params object[] constructorArguments)
{
    string assemblyName = Assembly.GetAssembly(typeof (T)).FullName;
    string typeName = typeof (T).FullName;

    //also tried this for no-argument constructors
    //var objectHandle = domain.CreateInstance(assemblyName, typeName );

    var objectHandle = domain.CreateInstance(assemblyName, typeName, false
        , BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance
        , null, constructorArguments, null, null, null);

    //This call fails with the exception: "Type is not resolved for member"
    return (T) objectHandle.Unwrap();
}

我失踪了什么?

最佳回答

Have you loaded the assembly defining type T in the domain? (You need to have the appropriate assembly and its dependencies loaded in the AppDomain before you can attempt to instantiate a type). Try instantiatiing a type defined in mscorlib or system to see if this is the issue.

问题回答

Hmm... I think MaLio is right. Additionally i would suggest, that you use a IoC in each Domain, like AutoFac. with that, you could even register Factories for a class. or different lifetimes. i do not like, that you have the object in first hand, and then pass it as a string. there has to be a better way.





相关问题
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. ...

热门标签