关于 Activator.CreateInstance的一个问题

Sq-Zyz123 2014-11-21 11:27:51
  object comObj = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")));
object obj = comObj.GetType().InvokeMember(_methodName, System.Reflection.BindingFlags.InvokeMethod, null, comObj, param);

在实例化的这个对象以后,我应该怎么判断这个Guid("30FB1E22-B498-4B62-8B26-98BA61176024"))类里面到底有没有_methodName这个方法啊?
...全文
525 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 18 楼 lovelj2012 的回复:
[quote=引用 15 楼 u013063561 的回复:] [quote=引用 14 楼 lovelj2012 的回复:] GetMethod用法,是不是2#搞错了。 照着msdn的例子用 http://msdn.microsoft.com/zh-cn/library/6hy0h0z1(v=vs.100).aspx
额,这个getMethod是调用方法啊?我要的只查询出这个方法是否存在于这个类中!· = = [/quote] 无语了~~~ GetMethod从类型里面反射出某个方法是否存在 类型.GetMethod返回结果不是null,就证明这个类型中存在这个方法了[/quote] 不是吧?我看csdn里面是写的方法名加参数,得到的是返回结果额? 但是我那个类中确实存在那个方法。method还是为空!~
江南小鱼 2014-11-21
  • 打赏
  • 举报
回复
引用 15 楼 u013063561 的回复:
[quote=引用 14 楼 lovelj2012 的回复:] GetMethod用法,是不是2#搞错了。 照着msdn的例子用 http://msdn.microsoft.com/zh-cn/library/6hy0h0z1(v=vs.100).aspx
额,这个getMethod是调用方法啊?我要的只查询出这个方法是否存在于这个类中!· = = [/quote] 无语了~~~ GetMethod从类型里面反射出某个方法是否存在 类型.GetMethod返回结果不是null,就证明这个类型中存在这个方法了
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 9 楼 feiyun0112 的回复:
comObj.GetType().GetMembers() 看有哪些MemberName
没用啊版主,找不到那个guid类当中的那些方法!~~~
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复

  args[0] = "DVC_CutImgmethod";
                args[1] = @"11";
                args[2] = "1";
                args[3] = "2";
                //如果没有数据的话,直接return
                if (args.Length == 0)
                {
                    return;
                }
                //获取方法名
                string _methodName = args[0];
                //创建list集合用于保存参数
                List<object> list = new List<object>();

                for (int i = 1; i < args.Length; i++)
                {
                    list.Add(args[i]);
                }
                //转换成object数组
                object[] param = list.ToArray();
            
                //通过GUID获取到DVBusiUtilCalClass类
                var type=Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024"));
                //查看是否有—_methodName这个方法
                var method = type.GetMethod(_methodName);
               MethodInfo[] sss=  type.GetMethods();
                //如果在DVBusiUtilCalClass没有找到这个方法,那么去创建DVBusiUtilPreClass的实例
                if (method == null)
                {
                    type = Type.GetTypeFromCLSID(new Guid("592703AB-CF61-425D-9ACD-A0938E451AEA"));
                }
                object comObj = Activator.CreateInstance(type);
                //创建类型实例
                //object comObj = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")));
                //MemberInfo[] vvv = comObj.GetType().GetMembers();
                //int ddd = vvv.Length;
                //if (comObj.GetType().GetMember(_methodName)==null)
                //{
                //    comObj = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("592703AB-CF61-425D-9ACD-A0938E451AEA")));
                //}
                //获取返回值
                object result = comObj.GetType().InvokeMember(_methodName, System.Reflection.BindingFlags.InvokeMethod, null, comObj, param);
               
                Console.WriteLine(Convert.ToInt32(result));
整体代码。。
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 14 楼 lovelj2012 的回复:
GetMethod用法,是不是2#搞错了。 照着msdn的例子用 http://msdn.microsoft.com/zh-cn/library/6hy0h0z1(v=vs.100).aspx
额,这个getMethod是调用方法啊?我要的只查询出这个方法是否存在于这个类中!· = =
江南小鱼 2014-11-21
  • 打赏
  • 举报
回复
GetMethod用法,是不是2#搞错了。 照着msdn的例子用 http://msdn.microsoft.com/zh-cn/library/6hy0h0z1(v=vs.100).aspx
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 11 楼 lovelj2012 的回复:
[quote=引用 8 楼 u013063561 的回复:] [quote=引用 6 楼 lovelj2012 的回复:] [quote=引用 3 楼 u013063561 的回复:] [quote=引用 2 楼 dongxinxi 的回复:] 反射呗 var type = Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")); var method = type.GetMethod(_methodName, 是否区分大小写); if(method != null) { method.Invoke(XXX) }
method值一直为空额,哪怕里面有那个方法也是[/quote] type有值没?[/quote] type一直没值!~~[/quote] type一直没值,那执行GetMethod肯定反射不来东东啊 是不是你的GUID搞错了[/quote] type有值,method没值!~一直是空
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 9 楼 feiyun0112 的回复:
comObj.GetType().GetMembers() 看有哪些MemberName
{System.String ToString()} System.Object GetLifetimeService System.Runtime.Remoting.ObjRef CreateObjRef(System.Type) 等等一共6个值!~
江南小鱼 2014-11-21
  • 打赏
  • 举报
回复
引用 8 楼 u013063561 的回复:
[quote=引用 6 楼 lovelj2012 的回复:] [quote=引用 3 楼 u013063561 的回复:] [quote=引用 2 楼 dongxinxi 的回复:] 反射呗 var type = Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")); var method = type.GetMethod(_methodName, 是否区分大小写); if(method != null) { method.Invoke(XXX) }
method值一直为空额,哪怕里面有那个方法也是[/quote] type有值没?[/quote] type一直没值!~~[/quote] type一直没值,那执行GetMethod肯定反射不来东东啊 是不是你的GUID搞错了
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 6 楼 lovelj2012 的回复:
[quote=引用 3 楼 u013063561 的回复:] [quote=引用 2 楼 dongxinxi 的回复:] 反射呗 var type = Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")); var method = type.GetMethod(_methodName, 是否区分大小写); if(method != null) { method.Invoke(XXX) }
method值一直为空额,哪怕里面有那个方法也是[/quote] type有值没?[/quote] type有值 但是method一直为空。。
feiyun0112 2014-11-21
  • 打赏
  • 举报
回复
comObj.GetType().GetMembers() 看有哪些MemberName
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 6 楼 lovelj2012 的回复:
[quote=引用 3 楼 u013063561 的回复:] [quote=引用 2 楼 dongxinxi 的回复:] 反射呗 var type = Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")); var method = type.GetMethod(_methodName, 是否区分大小写); if(method != null) { method.Invoke(XXX) }
method值一直为空额,哪怕里面有那个方法也是[/quote] type有值没?[/quote] type一直没值!~~
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 5 楼 u013063561 的回复:
[quote=引用 2 楼 dongxinxi 的回复:] 反射呗 var type = Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")); var method = type.GetMethod(_methodName, 是否区分大小写); if(method != null) { method.Invoke(XXX) }
   //通过GUID获取到DVBusiUtilCalClass类
             var type=Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024"));
                //查看是否有—_methodName这个方法
                var method = type.GetMethod(_methodName);
                如果在DVBusiUtilCalClass没有找到这个方法,那么去创建DVBusiUtilPreClass的实例
                if (method == null)
                {
                   type = Type.GetTypeFromCLSID(new Guid("592703AB-CF61-425D-9ACD-A0938E451AEA"));
                }
[/quote] type有值,但是method每值,一直是空
江南小鱼 2014-11-21
  • 打赏
  • 举报
回复
引用 3 楼 u013063561 的回复:
[quote=引用 2 楼 dongxinxi 的回复:] 反射呗 var type = Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")); var method = type.GetMethod(_methodName, 是否区分大小写); if(method != null) { method.Invoke(XXX) }
method值一直为空额,哪怕里面有那个方法也是[/quote] type有值没?
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 2 楼 dongxinxi 的回复:
反射呗 var type = Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")); var method = type.GetMethod(_methodName, 是否区分大小写); if(method != null) { method.Invoke(XXX) }
   //通过GUID获取到DVBusiUtilCalClass类
             var type=Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024"));
                //查看是否有—_methodName这个方法
                var method = type.GetMethod(_methodName);
                如果在DVBusiUtilCalClass没有找到这个方法,那么去创建DVBusiUtilPreClass的实例
                if (method == null)
                {
                   type = Type.GetTypeFromCLSID(new Guid("592703AB-CF61-425D-9ACD-A0938E451AEA"));
                }
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 1 楼 feiyun0112 的回复:
comObj.GetType().GetMember(_methodName)
不行啊,判断不出来啊,是否有这个方法
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 2 楼 dongxinxi 的回复:
反射呗 var type = Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")); var method = type.GetMethod(_methodName, 是否区分大小写); if(method != null) { method.Invoke(XXX) }
method值一直为空额,哪怕里面有那个方法也是
  • 打赏
  • 举报
回复
反射呗 var type = Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024")); var method = type.GetMethod(_methodName, 是否区分大小写); if(method != null) { method.Invoke(XXX) }
feiyun0112 2014-11-21
  • 打赏
  • 举报
回复
comObj.GetType().GetMember(_methodName)
Sq-Zyz123 2014-11-21
  • 打赏
  • 举报
回复
引用 31 楼 dongxinxi 的回复:
你把DispatchUtility这个类复制到你的项目中,然后用 if (!DispatchUtility.ImplementsIDispatch(obj)) { throw new ArgumentException("The object created for " + progId + " doesn't implement IDispatch."); } // See if we can get Type info and then do some reflection. Type dispatchType = DispatchUtility.GetType(obj, false); int dispId; if (dispatchType != null) { var hr = DispatchUtility.TryGetDispId(obj, _methodName, out dispId); return hr== 0 && dispId != -1; } obj就是com对象,其实是不需要obj的,有var type = Type.GetTypeFromCLSID(new Guid("30FB1E22-B498-4B62-8B26-98BA61176024"))就行了 如果这都看不懂,那你只好去补一下基础了
结贴,谢谢了。
加载更多回复(11)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧