我有一個命名空間中定義一個Employee類和代碼段經由反射是這樣調用通過反射接受一個代表作爲輸入
delegate bool IsPromotable (Employee employee);
class Program
{
public static void Main(string[] args)
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee() { EmployeeId = 1, EmployeeName = "A", Salary = 7500, Experience = 2 });
empList.Add(new Employee() { EmployeeId = 2, EmployeeName = "B", Salary = 11500, Experience = 6 });
empList.Add(new Employee() { EmployeeId = 3, EmployeeName = "C", Salary = 14500, Experience = 5 });
empList.Add(new Employee() { EmployeeId = 4, EmployeeName = "D", Salary = 10500, Experience = 7 });
IsPromotable isPromotableObj = new IsPromotable(EnableToPromote);
Employee EmployeeObj = new Employee();
EmployeeObj.PromoteEmployees(empList, isPromotableObj);
}
public static bool EnableToPromote(Employee employee)
{
if (employee.Experience >= 5 && employee.Salary >= 10000)
{
return true;
}
else
return false;
}
}
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 (isPromotableObj(employee))
{
Console.WriteLine(" {0} will be promoted in the next R.P. Cycle ", employee.EmployeeName);
}
}
Console.ReadKey();
}
}
現在從一個單獨的類在一個單獨的名稱空間中的方法我想要獲得類型以及與之相關的所有其他類型的對象。另外我想調用PromoteEmployee()方法。我怎麼做 ?
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
object employeeInstance = Activator.CreateInstance(employeeType);//Create an instance of the Employee Class Type
//sets it's properites
Console.WriteLine("***************Setting External assembly propeties for the second instance*************");
object employeeInstance2 = Activator.CreateInstance(employeeType);
//set's it's properties
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 agrsDel = new Object[] {(employeeInstance) => return employeeInstance};
dynamic value = employeeType.InvokeMember("PromoteEmployees", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, employeeInstance, new Object[] {employeeInstanceList,args})});
Console.ReadKey();
但我得到的錯誤是agrsDel不能隱蔽lambda表達式的一個對象,其次對象不包含對僱員的定義。我錯過了什麼?
如果你想知道爲什麼沒有任何反應,甚至沒有評論,你的代碼是非常難讀......怎麼樣簡化它並只發布重現問題所需的代碼? – Eser
就此工作。現在希望得到迴應。感謝您指出@Eser – StrugglingCoder
'agrsDel'變量的用途是什麼?你沒有在代碼中使用它。你也應該嘗試'employeeType.GetMethod(「PromoteEmployees」)。調用(...)' – thepirat000