我正在尝试一个反射的演示。我想在这个类中反映的程序集就是这样的
namespace DelegatesSampleApplication
{
delegate bool IsPromotable (Employee employee); // Declaration Syntax is similar to that of a method's
class Program
{
public static void Main(string[] args)
{
//code goes here
}
}
class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int Salary { get; set; }
public int Experience { get; set; }
public void PromoteEmployees(List<Employee> employeeList, IsPromotable isPromotableObj)
{
/*foreach (Employee employee in employeeList)
{
if (employee.Experience >= 5 && employee.Salary >= 10000) //That's a hard-coded logic that you have developed as a Framework Developer which makes the class itself not reusable
{
Console.WriteLine(" {0} will be promoted in the next R.P. Cycle ", employee.EmployeeName);
}
}
Console.ReadKey();*/
foreach (Employee employee in employeeList)
{
if (isPromotableObj(employee))
{
Console.WriteLine(" {0} will be promoted in the next R.P. Cycle ", employee.EmployeeName);
}
}
Console.ReadKey();
}
}
}
现在我面临的问题是,我正在尝试从我的程序中的这个程序集中读取并尝试调用以类实例作为参数的委托。 我正在做的事情就像在另一个类别中一样
namespace ReflectionSample
{
delegate bool empIsPromotable (Object obj);
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***************Loading External assembly*************");
Assembly assembly = Assembly.LoadFrom(@"C:\Users\Chiranjib\Documents\visual studio 2012\Projects\DelegatesSampleApplication\DelegatesSampleApplication\bin\Debug\DelegatesSampleApplication.exe");
Type employeeType = assembly.GetType("DelegatesSampleApplication.Employee"); //Gets the System.Type object for the Employee Class from the just loaded assembly with all it's dependencies
Console.WriteLine("***************Loading External assembly properties*************");
//Setting properties here
Console.WriteLine("***************Creating an array list that will hold these employee instances***************");
List<Object> employeeInstanceList = new List<object>();
employeeInstanceList.Add(employeeInstance);
employeeInstanceList.Add(employeeInstance2);
Console.WriteLine("***************Invoking External assembly methods*************");
var args = new Object[] {
new List<employeeType>(),
(((employeeInstance) => { return true; }))
};
Console.ReadKey();
}
}
}
这会引发错误。说employeeType缺少程序集引用。此外,我无法将lambda表达式转换为对象。我不能直接输入到(IsPromotable)吧?我正在使用反射。所以我不应该直接访问。
我如何通过反射访问代理? 请帮帮我。提前谢谢。
答案 0 :(得分:3)
IsPromotable
不可调用,它只是一个委托定义。把它想象成一个界面。它只是告诉你方法采用和返回的内容,但实际上并没有做任何事情。
您可以像这样致电PromoteEmployees
:
PromoteEmployees(new List<Employee>, (employee) => { return employee.Name == "Rob"; });
与IsPromotable
的签名匹配的任何方法都是该方法的有效参数(即,采用Employee
并返回bool
的任何方法)。你能详细说明一下你正在尝试做些什么吗?
如果您只想要一个代表列表,您可以这样做:
GetType().Assembly.GetTypes().Where(t => t.IsSubClassOf(typeof(BaseDelegate)));
请注意;这只会返回当前程序集中的委托。您可能希望将其更改为:
typeof(Employee).GetTypes().Assembly.Where(t => t.IsSubClassOf(typeof(BaseDelegate)));
要使程序集中的所有方法都可以转换为IsPromotable
,您可以执行以下操作:
var delegateMethod = typeof(IsPromotable).GetMethod("Invoke");
var @params = delegateMethod.GetParameters();
var returnType = delegateMethod.ReturnType;
var matchingMethods = typeof(IsPromotable)
.Assembly
.GetTypes()
.SelectMany(t => t.GetMethods())
.Where(m => {
if (m.ReturnType != returnType)
return false;
var currParams = m.GetParameters();
if (currParams.Length != @params.Length)
return false;
for(var i = 0; i < currParams.Length;i++)
if (currParams[i] != @params[i])
return false;
return true;
});
要使用反射调用方法,可以执行以下操作:
var args = new Object[] {
new List<Employee>(),
((IsPromotable)((emp) => { return true; }))
};
var value = employeeType.InvokeMember("PromoteEmployees", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, employeeInstance, args);
或者,您可以通过常规方法传递它:
var args = new Object[] {
new List<Employee>(),
((IsPromotable)Test)
};
var value = employeeType.InvokeMember("PromoteEmployees", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, employeeInstance, args);
使用方法:
private bool Test(Employee emp)
{
return false;
}