简体   繁体   English

从原始类型创建服务引用代理类型的实例?

[英]Create Instance of Service Reference Proxy Type from Original Type?

Given a Type, say Apple , I want to create the equivalent Type that would be created if Apple were to be auto-generated as a Service Reference Proxy Type. 给定一个类型,例如Apple ,我想创建一个等效的类型,如果要自动将Apple生成为服务引用代理类型,则会创建该类型。

So, Apple has a Type of AwesomeWebService.Apple , but when Studio generates proxy classes for the AwesomeWebService service, the Type it creates is something local to the assembly that holds the proxy. 因此, Apple具有Type of AwesomeWebService.Apple ,但是Studio为AwesomeWebService服务生成代理类时,它创建的Type是保存代理的程序集的本地内容。

It becomes something like OtherAssembly.AwesomeWebServiceProxyReference.Apple 它变成类似于OtherAssembly.AwesomeWebServiceProxyReference.Apple

The trick is that I don't know until runtime what proxy Type I want to create ( Apple or otherwise). 诀窍是直到运行时我才知道要创建哪种代理类型( Apple或其他)。 This would be too easy. 这太容易了。

I do have an instance of the Service Reference Client, say OtherAssembly.AwesomeWebServiceProxyReference.AwesomeWebServiceSoapClient , but I have so far been unable to use this information along with the original Type to get the proxy Type that I am after. 我确实有一个Service Reference Client的实例,例如OtherAssembly.AwesomeWebServiceProxyReference.AwesomeWebServiceSoapClient ,但是到目前为止,我一直无法使用此信息以及原始类型来获取我想要的代理类型。 When I try I get an error that the Type could not be found in the assembly. 当我尝试时,出现一个错误,表明在程序集中找不到Type。

Activator.CreateInstance("OtherAssemblyName", "TypeNameOfOriginalType");

where "OtherAssemblyName" is gathered from proxyClientType.GetType().Assembly.FullName and "TypeNameOfOriginal" is originalType.GetType().Name 其中“ OtherAssemblyName”是从proxyClientType.GetType().Assembly.FullName收集的,而“ TypeNameOfOriginal”是originalType.GetType().Name

Activator.CreateInstance("OtherAssembly", "Apple");

Figured it out. 弄清楚了。

Easiest way was just to get the Method I was trying to create the object for (parameter), get the ParameterInfo object, and get the exact Type from that. 最简单的方法就是获取我试图为其创建(参数)对象的方法,获取ParameterInfo对象,并从中获取确切的Type。

Type t = proxy.GetType();
MethodInfo m = t.GetMethodInfo("methodName");
ParameterInfo[] mParams = m.GetParameters();
string typeName = mParam.ParameterType.AssemblyQualifiedName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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