基本信息
源码名称:C#反射调用
源码大小:0.03M
文件格式:.rar
开发语言:C#
更新时间:2016-04-26
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class PrivateField
{
public delegate bool ReturnBoolHandler(string info);
public delegate void TestHandler(string msg);
public event ReturnBoolHandler ReturnBoolEvent;
public event TestHandler TestEvent;
}
class Program
{
public static void s_TestEvent(string msg)
{
Console.WriteLine(msg);
}
static void Main(string[] args)
{
Assembly ass = Assembly.Load("ConsoleApplication1");
object structInstance = ass.CreateInstance("ConsoleApplication1.PrivateField");
Type structType = structInstance.GetType();
EventInfo eventInfo = structType.GetEvent("TestEvent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
Type tDelegate = eventInfo.EventHandlerType;
MethodInfo miHandler = typeof(Program).GetMethod("s_TestEvent", BindingFlags.Public | BindingFlags.Static);
Delegate d = Delegate.CreateDelegate(tDelegate, miHandler);
MethodInfo miAddHandler = eventInfo.GetAddMethod();
object[] addHandlerArgs = { d };
miAddHandler.Invoke(structInstance, addHandlerArgs);
FieldInfo _Field = structType.GetField("TestEvent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
if (_Field != null)
{
object _FieldValue = _Field.GetValue(structInstance);
if (_FieldValue != null && _FieldValue is Delegate)
{
Delegate _ObjectDelegate = _FieldValue as Delegate;
_ObjectDelegate.DynamicInvoke(new object[] { "Sandy" });
}
}
}
}
}