简体   繁体   English

反射-在指定的类名上动态调用方法

[英]Reflection - Calling Method dynamically on specified Class Name

I need to use Reflection concept of Dot Net and I don't have much expertise on it. 我需要使用Dot Net的Reflection概念,但是我对此没有太多专业知识。 I will get class & method name as string and need to invoke that method on class. 我将以字符串形式获取类和方法的名称,并需要在类上调用该方法。

Following is the class on which I need to invoke method: 以下是我需要在其上调用方法的类:

namespace ObjectRepositories
{
    class Page_MercuryHome : CUITe_BrowserWindow
    {
        public new string sWindowTitle = "";
        public Page_MercuryHome()
        {
            #region Search Criteria
            this.SearchProperties[UITestControl.PropertyNames.Name] = "Blank Page";
            this.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
            this.WindowTitles.Add("Blank Page");
            this.WindowTitles.Add("Welcome: Mercury Tours");
            #endregion
        }

        public CUITe_HtmlEdit UIEdit_UserName = new CUITe_HtmlEdit("Name=userName");
        public CUITe_HtmlEdit UIEdit_Password = new CUITe_HtmlEdit("Name=password");
        public CUITe_HtmlInputButton UIInputButton_Login = new CUITe_HtmlInputButton("Name=login");
}
}

Now in the following method, I will receive Parent Class Name, Sub Class Name and Method to be called. 现在,在以下方法中,我将收到父类名称,子类名称和要调用的方法。

Like following: 如下所示:

void PerformOperation(string ParentClass, string SubClass, string MethodName)
{
/* Suppose if I receive arguments as "Page_MercuryHome","CUITe_BrowserWindow","SetText")
then it should call SetText() method of subclass CUITe_BrowserWindow which is having Page_MercuryHome as Parent Class"
}

I tried a lot doing this but not able to do. 我做了很多尝试,但没有做到。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks. 谢谢。

I assume that you know that the class will always be in the same assembly as PerformOperation. 我假设您知道该类将始终与PerformOperation在同一程序集中。 If it is not then you need the name of the assembly where it is located. 如果不是,则需要它所在的程序集的名称。

You don't need the ParentClass, but you do need the full name of the SubClass (including the namespace). 您不需要ParentClass,但确实需要SubClass的全名(包括名称空间)。

I assume that your class will always have a default constructor and that your method will never have any parameters. 我假设您的类将始终具有默认的构造函数,并且您的方法将永远不具有任何参数。

If the above holds true, the following should work: 如果上述情况成立,则应进行以下操作:

void PerformOperation(string fullClassName, string methodName)
{
   ObjectHandle handle = Activator.CreateInstance(null, fullClassName);
   Object p = handle.Unwrap();
   Type t = p.GetType();

   MethodInfo method = t.GetMethod(methodName);
   method.Invoke(p, null);
}

object ReadField(string fullClassName, string fieldName)
{
   ObjectHandle handle = Activator.CreateInstance(null, fullClassName);
   Object p = handle.Unwrap();
   Type t = p.GetType();

   FieldInfo field = t.GetField(fieldName);
   return field.GetValue(p);
}

Edit: added method to instantiate an object of a given class and returning the value of a given field on it 编辑:添加的方法实例化给定类的对象并在其上返回给定字段的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM