使用自定义类作为参数从 MethodInfo 调用方法

发布于 2024-12-31 22:27:00 字数 536 浏览 4 评论 0原文

我有这样的场景: 一个具有一些属性的自定义类(客户),如下所示:

public class Customer
{
    public int Handler { get; set; }
    public string Name { get; set; }
}

一个具有方法的自定义类,如下所示:

public class CustomerMethods
{
    public static void Insert(Customer customer)
    {
        //Do Something...
    }
}

因此,我将加载一个包含一些信息的文本文件,例如类名称、属性名称和属性值。 但是,真正的问题是,在设置 Handler 和 Name 属性的值后,如何从 CustomerMethods 类调用 Insert 方法并将 Customer 类作为参数传递?

哦,我差点忘了,我试图避免条件语句,因为我有 100 多个类。 /o\ 各位,如果您需要更多信息,请告诉我...

I have this scenario:
One custom class (Customer) with some properties, like this:

public class Customer
{
    public int Handler { get; set; }
    public string Name { get; set; }
}

One custom class with the Method, like this:

public class CustomerMethods
{
    public static void Insert(Customer customer)
    {
        //Do Something...
    }
}

So, I will load a text file with some info, like class name, property name and property value.
But, the real problem is, how can I invoke the Insert Method from CustomerMethods class and pass Customer class as parameter after set the values of Handler and Name properties?

Oh, I almost forget, I am trying to avoid conditionals, because I have 100+ classes. /o\
Ty all, if you need more info, just tell me plz...

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

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

发布评论

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

评论(2

洛阳烟雨空心柳 2025-01-07 22:27:00
typeof(CustomerMethods).GetMethod(SomeName).Invoke(null, new Customer(...))

但是,如果可能的话,您应该尝试重构您的设计并避免这种情况。

typeof(CustomerMethods).GetMethod(SomeName).Invoke(null, new Customer(...))

However, you should try to refactor your design and avoid this, if possible.

淡写薰衣草的香 2025-01-07 22:27:00

我仅使用这些字符串来调用静态插入方法 WindowsFormsApplication1.Form1+CustomerMethods      WindowsFormsApplication1.Form1+Customer     插入

Type customerMethodsType = Type.GetType("WindowsFormsApplication1.Form1+CustomerMethods");
Type customerType = Type.GetType("WindowsFormsApplication1.Form1+Customer");
object customerObject =  Activator.CreateInstance(customerType);

customerType.GetProperty("Handler").SetValue(customerObject, 3, null);

customerMethodsType.InvokeMember(
    "Insert",
    BindingFlags.Public | BindingFlags.InvokeMethod| BindingFlags.Static,
    null,
    null,
    new object[] { customerObject }
    );

I used only these strings to call static Insert method WindowsFormsApplication1.Form1+CustomerMethods     WindowsFormsApplication1.Form1+Customer     Insert

Type customerMethodsType = Type.GetType("WindowsFormsApplication1.Form1+CustomerMethods");
Type customerType = Type.GetType("WindowsFormsApplication1.Form1+Customer");
object customerObject =  Activator.CreateInstance(customerType);

customerType.GetProperty("Handler").SetValue(customerObject, 3, null);

customerMethodsType.InvokeMember(
    "Insert",
    BindingFlags.Public | BindingFlags.InvokeMethod| BindingFlags.Static,
    null,
    null,
    new object[] { customerObject }
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文