CSharp:如何动态调用类(带构造函数)及其方法?

发布于 2024-10-30 17:52:17 字数 962 浏览 6 评论 0原文

我已经阅读了两天的反射示例,但我似乎无法将所有内容拼凑起来以满足我的需要。事实上,我以为我正在使用反射,直到我注意到我的“使用 System.Reflection”呈灰色显示。 :-)

我有一个 xml 文件,其中包含我的类名和其中包含的方法。方法的顺序甚至名称都可以更改。一旦我读入它们,我希望能够执行它们。看起来很简单。

我有以下测试代码:

    // myClassName = "namespace.TransactionServices"
    Type tb = Type.GetType(myClassName);

    // Classes will always have the same constructor
    object classInstance = Activator.CreateInstance (
        tb, 
        new object[] 
        {
            authorization.apiKey, 
            authorization.userName, 
            testData.servicesEndPoint
        });

    //Grab the method I want

    //myMethodName = "GetVersion"
    MethodInfo mymethod = tb.GetMethod(myMethodName);
    // no parameters for this method call
    object result = mymethod.Invoke(classInstance, null);

tb 为空。我只是想努力尝试获得正确的 API 来创建该类,但我什至不知道我所拥有的其余内容是否有效。

有什么建议吗?

谢谢

编辑:添加了命名空间。 Activator.CreateInstance 返回错误,未找到构造函数。它就在那里。

I've been reading examples of Reflection for two days and I can't quite seem to piece together everything to fit what I need. In fact I thought I was using reflection, until I noticed my "using System.Reflection" was grayed out. :-)

I have an xml file that contains my class names and the methods contained within. The order of the methods or even the names can change. Once I read them in I want to be able to execute them. Seems simple enough.

I have the following test code:

    // myClassName = "namespace.TransactionServices"
    Type tb = Type.GetType(myClassName);

    // Classes will always have the same constructor
    object classInstance = Activator.CreateInstance (
        tb, 
        new object[] 
        {
            authorization.apiKey, 
            authorization.userName, 
            testData.servicesEndPoint
        });

    //Grab the method I want

    //myMethodName = "GetVersion"
    MethodInfo mymethod = tb.GetMethod(myMethodName);
    // no parameters for this method call
    object result = mymethod.Invoke(classInstance, null);

tb is null. I was just going to work away at trying to get the correct API for creating the class, but I don't even know if the rest of what I have is valid.

Any suggestions?

Thanks

Edit: Added namespace. Activator.CreateInstance is returning error that constructor is not found.. It is there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍风燃霜 2024-11-06 17:52:17

类名必须是完全限定的(包括命名空间,很可能还包括程序集)才能被解析。如果您知道该类位于当前执行的程序集中,则可以使用 Assembly.GetExecutingAssembly().GetType()

The class name must be fully qualified (including namespace and, most likely, the assembly) in order for it to be resolved. If you know the class is in your current executing assembly, you can use Assembly.GetExecutingAssembly().GetType()

忆沫 2024-11-06 17:52:17

您确定指定了完整的类名(包括命名空间)吗?如果没有命名空间,结果将为空。

Are you sure you specified the complete class name including namespace? without the namespace, the result will be null.

葬花如无物 2024-11-06 17:52:17

对于 GetType,您必须提供完全限定名称。您可以使用现有对象获取它

MyObject.GetType().AssemblyQualifiedName;

For GetType you have to provide a fully qualified name. You can get it for an existing object using

MyObject.GetType().AssemblyQualifiedName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文