C# 桌面软件开发-深入学习 [1]- AY-C#人爱学不学-aaronyang技术分享
原文: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#,”的文章
C# 桌面软件开发-深入学习 [1]- AY-C#人爱学不学-aaronyang技术分享的更多相关文章
- C# 桌面软件开发-深入学习[2]- AY-C#人爱学不学-aaronyang技术分享
原文:C# 桌面软件开发-深入学习[2]- AY-C#人爱学不学-aaronyang技术分享 1 : C# Assembly.GetEntryAssembly().GetName().Version. ...
- AY写给国人的教程- VS2017 Live Unit Testing[1/2]-C#人爱学不学-aaronyang技术分享
原文:AY写给国人的教程- VS2017 Live Unit Testing[1/2]-C#人爱学不学-aaronyang技术分享 谢谢大家观看-AY的 VS2017推广系列 Live Unit Te ...
- AY写给国人的教程- VS2017 Live Unit Testing[2/2]-C#人爱学不学-aaronyang技术分享
原文:AY写给国人的教程- VS2017 Live Unit Testing[2/2]-C#人爱学不学-aaronyang技术分享 谢谢大家观看-AY的 VS2017推广系列 Live Unit Te ...
- Mac 桌面软件开发基础问答
1> Mac OS X平台下的桌面软件是由什么编程语言处理 答: 由Objective-C, swift编程语言处理 2> Mac OS X平台下的桌面软件是由什么框架构建 答: 由Coc ...
- nw.js桌面软件开发系列 第0.1节 HTML5和桌面软件开发的碰撞
第0.1节 HTML5和桌面软件开发的碰撞 当我们谈论桌面软件开发技术的时候,你会想到什么?如果不对技术本身进行更为深入的探讨,在我的世界里,有这么多技术概念可以被罗列出来(请原谅我本质上是一个Win ...
- AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享
原文:AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享 ====================www.ayjs.net 杨洋 wpfui.com ...
- AY的Dapper研究学习-基本入门-C#开发-aaronyang技术分享
原文:AY的Dapper研究学习-基本入门-C#开发-aaronyang技术分享 ====================www.ayjs.net 杨洋 wpfui.com ...
- 基于Qt Designer和PyQt5的桌面软件开发--环境搭建和入门例子
本文介绍了如何使用技术栈PyCharm+Qt Designer+PyQt5来开发桌面软件,从环境搭建.例子演示到对容易混淆概念的解释.文中用到的全部软件+代码下载链接为:https://url39 ...
- 拿到这份 Java、C++ 软件开发完整学习路线图,我面试再也没挂过..
大家好,我是柠檬. 柠檬哥作为一个普通大学.非计算机专业,自学后端技术进入腾讯做后端开发工作,我自己也是非科班自学计算机成功转行软件开发(有想听柠檬哥转行之路经历的吗,可以留言告诉我,人多就写写),体 ...
随机推荐
- pycharm highlight
https://www.jetbrains.com/help/pycharm/2017.1/highlighting-usages.html Highlighting usages in the cu ...
- [tmux] Copy and paste text from a tmux session
One non-obvious, but extremely useful, feature in tmux is copy-pasting text between panes. This also ...
- [Javascript] Javascript 'in' opreator
If you want to check whether a key is inside an Object or Array, you can use 'in': Object: const obj ...
- Android应用程序文件缓存getCacheDir()和getExternalCacheDir()
如果Android引用程序需要缓存临时文件,系统提供了一个可管理的“内部缓存”和一个不可管理的“外部缓存”,分别调用getCacheDir()和getExternalCacheDir()方法,可以从当 ...
- asp.net core2.1 部署centos7/linux系统 -- 安装部署(一)
原文:asp.net core2.1 部署centos7/linux系统 -- 安装部署(一) 1.安装dotnet sdk(添加产品秘钥与yum源) 添加yum源:sudo rpm -Uvh htt ...
- SignalR+AForge实现视频会话[WPF]
原文:SignalR+AForge实现视频会话[WPF] 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/lordwish/article/detai ...
- ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案
原文:ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不大 ...
- 谈谈android缓存文件
##内部存储 总是可用的 这里的文件默认是只能被你的app所访问的. 当用户卸载你的app的时候,系统会把internal里面的相关文件都清除干净. Internal是在你想确保不被用户与其他app所 ...
- CentOS 挂载iso文件配置yum源
1.挂载iso 准备好centos的光盘镜像 挂载前的准备; mkdir -p /dev/centos mkdir -p /mnt/local_yum 挂载 mount -o loop /opt/s ...
- Compile Graphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64
Compile Graphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64 Sun, 01/01/2012 - 15:43 ...