如何调用泛型对象的已知方法

本文关键字:对象 方法 泛型 何调用 调用 | 更新日期: 2023-09-27 18:24:13

很抱歉找不到相关的SO问题

我使用反射来获得object的属性(它是另一个对象),使用:

public static T GetPropertyValue<T>(this object obj, string propertyName)
{
    PropertyInfo prop = obj.GetType().GetProperty(propertyName);
    return (T)prop.GetValue(obj, null);
}

我有一个(Xero)Api,看起来像:

public class XeroCoreApi : XeroApi
{
    public AccountsEndpoint Accounts { get; }
    public ContactsEndpoint Contacts { get; }
    // ...
}

其中Endpoint继承的类看起来像:

public abstract class XeroUpdateEndpoint
{
    public TResult Update(TResult item);
    // ...
}

即,我可以调用特定实体的更新:

Contacts.Update(...);

当我调用GetPropertyValue()方法时,我从XeroCoreApi的一个实例中获得Endpoint对象,但直到运行时我才知道它是方法(我确实知道,但编译器不知道)。

为了获得Endpoint,我运行类似于以下命令的命令:

var endpoint = _api.GetPropertyValue<object>("Contacts");
// For the sake of this example the "Contacts" is manually
// entered, violating the whole idea of generics

问题是我不能做类似于endpoint.Update(...)的事情(因为endpointvar,并且一些endpoint并没有特别继承Update()方法)。

是否可以使用反射来运行该方法?语法可能是什么样子的?

摘要:

如何使用反射调用类型为T的对象的方法(Update())(即,直到运行时我们才知道该对象)?

例如endpoint.Update(...)

如何调用泛型对象的已知方法

如果我理解正确,您需要泛型类型约束(而不是反射)。这为编译器提供了类型满足某些条件的证明。

例如,一个接口:

public interface IUpdateStuff {
    void Update();
}
public class XeroCoreApi : XeroApi, IUpdateStuff {
    // implementation here
}

然后你可以限制你的通用类型:

public TResult Update(TResult item) where TResult : IUpdateStuff ;

现在编译器将允许您:

public TResult Update(TResult item) where TResult : IUpdateStuff {
    item.Update(); // <-- this is okay now.
}

EDIT:这假设您的泛型类型来自封闭类。。这在你的例子中是显而易见的。