Which Activator.CreateInstance overload function to call?
Which Activator.CreateInstance overload function to call? I have a type returned from "Type proxyType = GetProxyType(contractType);" and the constructorinfo is
"[System.Reflection.RuntimeConstructorInfo] = {Void .ctor(System.ServiceModel.InstanceContext)} base {System.Reflecti开发者_如何转开发on.MemberInfo} = {Void .ctor(System.ServiceModel.InstanceContext)}
[System.Reflection.RuntimeConstructorInfo] = {Void .ctor(System.ServiceModel.InstanceContext, System.String)} base {System.Reflection.MethodBase} = {Void .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)} base {System.Reflection.MethodBase} = {Void .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)}.
Thanks!!
It seems the type has a default constructor so Activator.CreateInstance(proxyType);
should work. If you want to call some other constructor for example the one that take a string parameter you could do this:
var instance = Activator.CreateInstance(proxyType, "some string parameter");
or the one with two string parameters:
var instance = Activator.CreateInstance(proxyType, "param1", "param2");
UPDATE:
My mistake there's no parameterless constructor for this type defined. All constructors need at least one argument which is of type InstanceContext. So in order to create an instance of this type you will need to pass at least the instance context. For example if you are in a WCF you could try this:
var instance = Activator.CreateInstance(
proxyType,
OperationContext.Current.InstanceContext
);
精彩评论