samedi 22 août 2015

Accessing a delegate via reflection declared outside of any class?

I am trying a demo for reflection. The assembly I want to reflect in this class goes something like this

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();
        }
    }
}

Now the problem I am facing is, I am trying to read from this assembly in my program and trying to invoke the delegate which takes in a class instance as a parameter.

How would I be able to access the delegate via reflection ? As I suppose

Type.GetNestedTypes() 

wont help my cause as it is not contained within any sort of classes. Please help me through. Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire