您现在的位置是:首页 > 文章详情

C# 桌面软件开发-深入学习 [1]- AY-C#人爱学不学-aaronyang技术分享

日期:2018-06-05点击:364
原文: C# 桌面软件开发-深入学习 [1]- AY-C#人爱学不学-aaronyang技术分享

曾经我做office,不想依赖别人dll,就使用了

Type.GetTypeFromProgID 可以根据 一个 ID 获得office的操作对象了,当然你也可以获得其他的操作对象,这个id就像一个密码。

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

我AY的教程是按照我 高中的思维水平来理解的,如果有问题,还请指出。


写一个控制台或者 WPF程序


新建一个测试用的 类型

C#
 public class AyClass1 { public string P1 { get; set; } public int P2 { get; set; } public int M1() { return P2 * 2; } public int M2(int t) { return P2 * t; } public void M3() { } }
C#
 AyClass1 cls = new AyClass1(); RuntimeTypeHandle _r0 = cls.GetType().TypeHandle; RuntimeTypeHandle _r1 = Type.GetTypeHandle(cls); Console.WriteLine(_r0.Equals(_r1)); var _t = Type.GetTypeFromHandle(_r0); Console.WriteLine(_t==typeof(AyClass1));

image.png

image.png

看完上面一段代码,能有什么用呢, 我也没发现啥,  从类型实例获得Type,从 类获得Type,从类型句柄获得Type

C#
RuntimeTypeHandle FromObject = Type.GetTypeHandle(cls); RuntimeTypeHandle FromType = typeof(AyClass1).TypeHandle; Console.WriteLine("\nFromObject.Value: {0}", FromObject.Value); Console.WriteLine("FromObject.GetType(): {0}",FromObject.GetType()); Console.WriteLine("Get the type back from the handle..."); Console.WriteLine("Type.GetTypeFromHandle(FromObject): {0}", Type.GetTypeFromHandle(FromObject)); Console.WriteLine("\nFromObject.Equals(FromType): {0}", FromObject.Equals(FromType)); Console.WriteLine("\nFromType.Value: {0}", FromType.Value); Console.WriteLine("FromType.GetType(): {0}", FromType.GetType()); Console.WriteLine("Get the type back from the handle..."); Console.WriteLine("Type.GetTypeFromHandle(FromType): {0}", Type.GetTypeFromHandle(FromType));

image.png

也就是说,从类型实例,还是从 类型 获得类型句柄都是同一个对象的。

DUDU博客:查看

Type 是一个表示类型的抽象类;RuntimeType 是 Type 针对载入类型信息的具体实现;RuntimeTypeHandle 则是类型唯一的抽象句柄


有了Type,你就可以创建实例,然后调用使用了。



====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========


返回一个实例

C#
 var _1 = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(AyClass1)) as AyClass1; _1.P2 = 10; var _2 = _1.M2(10); MessageBox.Show(_2.ToString());//应该返回100

image.png

竟然说到这里了,看下反射

新建Test1类库,客户端引用下

C#
using System; namespace Test1 { public class Class1 { public string P1 { get; set; } public int M1(int t) { return 10 * t; } public void M3() { Console.WriteLine(P1+" M3被执行"); } } }

image.png

C#
 System.Runtime.Remoting.ObjectHandle handle = Activator.CreateInstance("Test1", "Test1.Class1"); Object p = handle.Unwrap(); Type t = p.GetType(); System.Reflection.PropertyInfo prop = t.GetProperty("P1"); if (prop != null) prop.SetValue(p, "AY"); System.Reflection.MethodInfo method = t.GetMethod("M3"); method.Invoke(p, null);

image.png


也可以这样创建object实例

C#
 Type t = Type.GetType("Test1.Class1,Test1"); Object p = Activator.CreateInstance(t);

那如果有构造函数的呢?

新增测试2

C#
 public class Class2 { public Class2(string hello) { this.P1 = hello; } public string P1 { get; set; } public int M1(int t) { return 10 * t; } public string M3() { return P1 + " M3被执行"; } }

添加执行代码

C#
Type t = Type.GetType("Test1.Class2,Test1"); Object p = Activator.CreateInstance(t, "aaronyang:"); System.Reflection.MethodInfo method = t.GetMethod("M3"); var _t = method.Invoke(p, null); MessageBox.Show(_t.ToString());

image.png

那么同样,如果方法有重载?泛型等,这种写不完的,具体,自己研究。


去掉类库的引用,拷贝生成的dll到 客户端的生成目录下,你知道了,不引用怎么创建类型调用

C#
 Type[] types = System.Reflection.Assembly.LoadFile(Environment.CurrentDirectory + "\\Test1.dll").GetTypes(); var type = types.FirstOrDefault(x => x.Name == "Class2"); if (type != null) { Object p = Activator.CreateInstance(type, "aaronyang:"); System.Reflection.MethodInfo method = type.GetMethod("M3"); var _t = method.Invoke(p, null); MessageBox.Show(_t.ToString()); }

效果同上。


你也可以这样写,效果同上

C#
 System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("Test1"); if (assembly != null) { Type type = assembly.GetType("Test1.Class2"); if (type != null) { Object p = Activator.CreateInstance(type, "aaronyang:"); System.Reflection.MethodInfo method = type.GetMethod("M3"); var _t = method.Invoke(p, null); MessageBox.Show(_t.ToString()); } }

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========



好了,附加参考

Assembly 通过此类可以加载操纵一个程序集,并获取程序集内部信息 

EventInfo 该类保存给定的事件信息 

FieldInfo 该类保存给定的字段信息 

MethodInfo 该类保存给定的方法信息 

MemberInfo 该类是一个基类,它定义了EventInfo、FieldInfo、MethodInfo、PropertyInfo的多个公用行为 

Module 该类可以使你能访问多个程序集中的给定模块 

ParameterInfo 该类保存给定的参数信息  

PropertyInfo 该类保存给定的属性信息

object[] typeAttributes=type.GetCustomAttributes(false); 自定义特性



====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

推荐您阅读更多有关于“C#,”的文章

原文链接:https://yq.aliyun.com/articles/678374
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章