2010-03-24 72 views
0

要調用哪個Activator.CreateInstance重載函數?我有一個從 返回的類型「Type proxyType = GetProxyType(contractType);」和constructorinfo是要調用哪個Activator.CreateInstance重載函數?

「[System.Reflection.RuntimeConstructorInfo] = {無效.ctor(System.ServiceModel.InstanceContext)} 基{System.Reflection.MemberInfo} = {空隙.ctor(System.ServiceModel.InstanceContext) }

[System.Reflection.RuntimeConstructorInfo] = {無效.ctor(System.ServiceModel.InstanceContext,System.String)} 基{System.Reflection.MethodBase} = {空隙.ctor(System.ServiceModel.InstanceContext, System.String)}

[System.Reflection.RuntimeConstructorInfo] = {Void .ctor(System.ServiceModel.InstanceContext,System.String,System.String)} base {System.Reflection.MethodBase} = {Void .ctor(System.ServiceModel.InstanceContext,System.String,System.String)}

[System.Reflection.RuntimeConstructorInfo] = {Void .ctor(System.ServiceModel .InstanceContext,System.String,System.ServiceModel.EndpointAddress)} 基{System.Reflection.MethodBase} = {空隙.ctor(System.ServiceModel.InstanceContext,System.String,System.ServiceModel.EndpointAddress)}

[System.Reflection.RuntimeConstructorInfo] = {Void .ctor(System.ServiceModel.InstanceContext,System.ServiceModel.Channels.Binding,System.ServiceModel.EndpointAddress)} base {System.Reflection.MethodBase} = {Void .ctor(System .ServiceModel.InstanceContext,System.ServiceModel .Channels.Binding,System.ServiceModel.EndpointAddress)}。

謝謝!

回答

0

看來這個類型有一個默認構造函數,所以Activator.CreateInstance(proxyType);應該工作。如果你想調用其他一些構造函數的例子,在字符串中的一個,你可以這樣做:

var instance = Activator.CreateInstance(proxyType, "some string parameter"); 

或一個有兩個字符串參數:

var instance = Activator.CreateInstance(proxyType, "param1", "param2"); 

UPDATE:

我的錯誤沒有無參數的構造函數定義這種類型。所有構造函數至少需要一個類型爲InstanceContext的參數。所以爲了創建這種類型的實例,您至少需要傳遞實例上下文。例如,如果你在一個WCF你可以試試這個:

var instance = Activator.CreateInstance(
    proxyType, 
    OperationContext.Current.InstanceContext 
); 
+0

謝謝達林。我以爲Activator.CreateInstance(proxyType)也是。但它引發了我「爲此對象定義的無參數構造函數」。在System.RuntimeType.CreateInstanceSlow(布爾publicOnly,布爾fillCache) (在System.RuntimeType.CreateInstanceImpl())上的堆棧報告位於System.RuntimeTypeHandle.CreateInstance(RuntimeType類型,Boolean publicOnly,Boolean noCheck,Boolean和canBeCached,RuntimeMethodHandle&ctor,Boolean&bNeedSecurityCheck)布爾publicOnly,布爾skipVisibilityChecks,布爾fillCache) .... – Don 2010-03-24 20:37:08

+0

請參閱我的更新。 – 2010-03-24 20:49:12

+0

達林,我們可以得到一個InstanceContext來執行proxyType嗎? OperationContext.Current.InstanceContext似乎是執行當前服務的一個。 – Don 2010-03-25 15:57:36