怎么传一个自定义的实体给MethodInfo下的Invoke函数

.Net技术 码拜 9年前 (2015-08-12) 1444次浏览
 

public class AAA

    {

        public string BBBB{ get; set; }

        public string CCCC { get; set; }

        public string DDDD { get; set; }

    }

比如传一个List<AAA>到另一个项目的web service。如何传?

把这个list放在一个obj数组里,会报类型“PwCERITRAdmin.ITREfilingObj[]”的对象无法转换为类型“ServiceBase.WebService.DynamicWebLoad.ITREfilingObj[]”。(ITREfilingObj是我定义的实体) 

#2
                    object list = Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(AAA)));
                    

                    object a = Activator.CreateInstance(typeof(AAA));
                    

                    list.GetType().GetMethod("Add").Invoke(list, new object[] { a });
#3

10分

一般需要把你定义的实体所在dll添加到客户端引用中才行,如果不这样做,那么就必须通过特性指定类的命名空间,让方法在序列化时使用统一的命名空间才可以通信。
#4

回复2楼:

还是没懂,这样写

                    

                        object list = Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(ITREfilingObj)));
                    

                         object a = Activator.CreateInstance(typeof(ITREfilingObj)); 
                    

                        list.GetType().GetMethod("Add").Invoke(list, new object[] { a });   
                    

怎么将List<ITREfilingObj> AAA赋值以后传到另外个项目

[WebMethod]

public bool InsertXmlData(ITREfilingObj[] strs){}

服务中

#5
添加web引用

然后调用

object list = Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(ITREfilingObj)));

object a = Activator.CreateInstance(typeof(ITREfilingObj)); 

list.GetType().GetMethod(“Add”).Invoke(list, new object[] { a });

webservice1.InsertXmlData((a as List<ITREfilingObj>).ToArray());

#6
我将List<ITREfilingObj> AAA赋值好以后,怎么赋给object a呢?
#7
webservice1.InsertXmlData((a as List<ITREfilingObj>).ToArray()); 

这个webservice1的对象怎么定义

#8

回复7楼:

添加web引用

#9

回复8楼:

using System.Web.Services;

这个么。那怎么会能直接点出.InsertXmlData

#10
项目-添加web引用。
#11
我把代码直接贴出来把

                    

                           //这个是用来访问服务的方法
                    

                           public class WebServiceHelper
                    

                            {
                    

                    

                                /// <summary> 
                    

                                /// 动态调用WebService 
                    

                                /// </summary> 
                    

                                /// <param name="methodname">方法名(模块名)</param> 
                    

                                /// <param name="args">参数列表</param> 
                    

                                /// <returns>object</returns> 
                    

                                public static object InvokeWebService(string methodname, object[] args)
                    

                                {
                    

                                    string url = System.Configuration.ConfigurationManager.AppSettings["B1EfilingService"];
                    

                                    return InvokeWebService(url, null, methodname, args);
                    

                    

                                }
                    

                    

                                /// <summary> 
                    

                                /// 动态调用WebService 
                    

                                /// </summary> 
                    

                                /// <param name="url">WebService地址</param> 
                    

                                /// <param name="classname">类名</param> 
                    

                                /// <param name="methodname">方法名(模块名)</param> 
                    

                                /// <param name="args">参数列表</param> 
                    

                                /// <returns>object</returns> 
                    

                                public static object InvokeWebService(string url, string classname, string methodname,object[] args)
                    

                                {
                    

                    

                                    string @namespace = "ServiceBase.WebService.DynamicWebLoad";
                    

                                    if (classname == null || classname == "")
                    

                                    {
                    

                                        classname = WebServiceHelper.GetClassName(url);
                    

                                    }
                    

                    

                                    //获取服务描述语言(WSDL) 
                    

                    

                                    WebClient wc = new WebClient();
                    

                    

                                    Stream stream = wc.OpenRead(url + "?WSDL");
                    

                    

                                    ServiceDescription sd = ServiceDescription.Read(stream);
                    

                    

                                    ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
                    

                    

                                    sdi.AddServiceDescription(sd, "", "");
                    

                                    CodeNamespace cn = new CodeNamespace(@namespace);
                    

                    

                                    //生成客户端代理类代码 
                    

                    

                                    CodeCompileUnit ccu = new CodeCompileUnit();
                    

                    

                                    ccu.Namespaces.Add(cn);
                    

                    

                                    sdi.Import(cn, ccu);
                    

                    

                                    CSharpCodeProvider csc = new CSharpCodeProvider();
                    

                    

                                    ICodeCompiler icc = csc.CreateCompiler();
                    

                    

                                    //设定编译器的参数 
                    

                    

                                    CompilerParameters cplist = new CompilerParameters();
                    

                    

                                    cplist.GenerateExecutable = false;
                    

                    

                                    cplist.GenerateInMemory = true;
                    

                    

                                    cplist.ReferencedAssemblies.Add("System.dll");
                    

                    

                                    cplist.ReferencedAssemblies.Add("System.XML.dll");
                    

                    

                                    cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
                    

                    

                                    cplist.ReferencedAssemblies.Add("System.Data.dll");
                    

                    

                                    //编译代理类 
                    

                    

                                    CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
                    

                    

                                    if (true == cr.Errors.HasErrors)
                    

                                    {
                    

                    

                                        System.Text.StringBuilder sb = new StringBuilder();
                    

                    

                                        foreach (CompilerError ce in cr.Errors)
                    

                                        {
                    

                    

                                            sb.Append(ce.ToString());
                    

                    

                                            sb.Append(System.Environment.NewLine);
                    

                    

                                        }
                    

                    

                                        throw new Exception(sb.ToString());
                    

                    

                                    }
                    

                    

                    

                    

                                    //生成代理实例,并调用方法 
                    

                    

                                    System.Reflection.Assembly assembly = cr.CompiledAssembly;
                    

                    

                                    Type t = assembly.GetType(@namespace + "." + classname, true, true);
                    

                    

                                    object obj = Activator.CreateInstance(t);
                    

                    

                                    System.Reflection.MethodInfo mi = t.GetMethod(methodname);
                    

                    

                                    return mi.Invoke(obj, args); //这是原本的传参方法,args只能是object[]类型,我想传一个自己定义的实体。
                    

                    

                                    object list = Activator.CreateInstance(typeof(List<>).MakeGenericType(typeof(ITREfilingObj)));
                    

                                    object a = Activator.CreateInstance(typeof(ITREfilingObj));
                    

                                    list.GetType().GetMethod("Add").Invoke(list, new object[] { a });
                    

                    

                    

                                    //return webservice1.Invoke(obj, (a as List<ITREfilingObj>).ToArray());//我已经引用了System.Web.Services
                    

                    

                                }
                    

                    

                                private static string GetClassName(string url)
                    

                                {
                    

                                    string[] parts = url.Split(""/"");
                    

                                    string[] pps = parts[parts.Length - 1].Split(""."");
                    

                                    return pps[0];
                    

                    

                                }
                    

                    

                            }
                    

                    //这些是用来调用的--itrEfilingObjs 是已经赋值好的List<ITREfilingObj> itrEfilingObjs
                    

                                 object[] dataObj = new object[] { itrEfilingObjs };
                    

                                 (bool)PwCERITRCommon.WebServiceHelper.InvokeWebService("InsertXmlData", dataObj)//InsertXmlData是我要访问的服务,返回bool
                    

                    

我不知道怎讲么将这个itrEfilingObjs 传到服务段

#14
这样写mi.Invoke(obj, args); 这句 会报 类型“System.Collections.Generic.List`1[PwCERITRCommon.ITREfilingObj]”的对象无法转换为类型“ServiceBase.WebService.DynamicWebLoad.ITREfilingObj[]”。的错误

为什么

#17
把这个序列化成json 对象 不行吗?
#18

30分

给你个代码示例,再看不懂就别怪我了,自己的悟性问题:

                    [DataContract(Namespace="")]
                    

                    public class AAA
                    

                    {
                    

                        [DataMember]
                    

                        public string BBBB{ get; set; }
                    

                        [DataMember]
                    

                        public string CCCC { get; set; }
                    

                        [DataMember]
                    

                        public string DDDD { get; set; }
                    

                    }
#19

回复3楼:

我在两个系统中都生成了同样的obj也不行?我已经解决了- -感谢

#21
Type mm = a.GetType(“命名空间+类名”);

var currentMethod = mm.GetMethod(“方法名”);

var args = currentMethod.GetParameters();//方法参数列表,我这里只有一个参数

var o1 = Activator.CreateInstance(args[0].ParameterType);

var result = currentMethod.Invoke(o, new object[] { o1, 0 });

给o1赋值没试,就这样拉


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么传一个自定义的实体给MethodInfo下的Invoke函数
喜欢 (0)
[1034331897@qq.com]
分享 (0)