C# 桌面软件开发-深入学习 [1]- AY-C#人爱学不学-aaronyang技术分享
曾经我做office,不想依赖别人dll,就使用了
Type.GetTypeFromProgID 可以根据 一个 ID 获得office的操作对象了,当然你也可以获得其他的操作对象,这个id就像一个密码。
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
我AY的教程是按照我 高中的思维水平来理解的,如果有问题,还请指出。
写一个控制台或者 WPF程序
新建一个测试用的 类型
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() { } }
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));
看完上面一段代码,能有什么用呢, 我也没发现啥, 从类型实例获得Type,从 类获得Type,从类型句柄获得Type
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));
也就是说,从类型实例,还是从 类型 获得类型句柄都是同一个对象的。
DUDU博客:查看
Type 是一个表示类型的抽象类;RuntimeType 是 Type 针对载入类型信息的具体实现;RuntimeTypeHandle 则是类型唯一的抽象句柄
有了Type,你就可以创建实例,然后调用使用了。
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
返回一个实例
var _1 = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(AyClass1)) as AyClass1; _1.P2 = 10; var _2 = _1.M2(10); MessageBox.Show(_2.ToString());//应该返回100
竟然说到这里了,看下反射
新建Test1类库,客户端引用下
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被执行"); } } }
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);
也可以这样创建object实例
Type t = Type.GetType("Test1.Class1,Test1"); Object p = Activator.CreateInstance(t);
那如果有构造函数的呢?
新增测试2
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被执行"; } }
添加执行代码
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());
那么同样,如果方法有重载?泛型等,这种写不完的,具体,自己研究。
去掉类库的引用,拷贝生成的dll到 客户端的生成目录下,你知道了,不引用怎么创建类型调用
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()); }
效果同上。
你也可以这样写,效果同上
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#,”的文章
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Protostuff 的Java序列化和反序列化
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34173549/article/details/80589509 <dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-core</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-runtime</artifactId> <version>1.4.0</version> </dependency> import java.util.Map; import io.protostuff.LinkedBuffer; import...
- 下一篇
C# 桌面软件开发-深入学习[2]- AY-C#人爱学不学-aaronyang技术分享
原文: C# 桌面软件开发-深入学习[2]- AY-C#人爱学不学-aaronyang技术分享 1 : C# Assembly.GetEntryAssembly().GetName().Version.ToString() 获得值是2.1.0.0 版本这东西,做升级,错误记录 都会有用的。 ====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。========= 2:创建ini文件 C# [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filepath); C# string section = "runVersion"; string key = "Net45"; DateTime d = TimeZone.CurrentTimeZone.ToLocalTime(new...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS关闭SELinux安全模块
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Hadoop3单机部署,实现最简伪集群
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS6,CentOS7官方镜像安装Oracle11G
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8编译安装MySQL8.0.19
- CentOS6,7,8上安装Nginx,支持https2.0的开启