对对象使用 C# 动态方法

发布于 2024-11-19 20:02:37 字数 1172 浏览 1 评论 0原文

我有一个方法应该从列表中返回 id。 通常我会使用反射来完成此任务(我不能使用通用方法,因为这些类通常是不共享接口或基类的 POCOS,并且我无法修改它们)。但是,我想到了新的 dynamic 关键字并想尝试一下。

然而我的问题是 dataSource[index] 返回一个对象。那么在运行时,可以确保对象本身属于我自己的类并且具有 id 属性。 但我想因为该方法返回一个对象,所以我在运行时访问 current.id 时收到 RumtineBinderException

public List<int> GetItemIds()
{

    var result = new List<int>();
    var dataSource = GetDataSource(); // returns an List<Object>

    for (int i = 0; i <= dataSource.Count - 1; i++)
    {
        dynamic current = dataSource[i];
        int id = current.Id;  // throws RuntimeBinderException: Object has no definition for id
    }

    return result;
}

有没有办法实现我想要的,或者我必须返回反射来获取 id财产?

更新:

current.GetType() returns object
current.GetType().GetProperties() returns a TargetInvocationException

我的 Pocos 位于我的主项目(VB.net)中,但此方法位于类库中,也许这就是原因。但是:

object current = dataSource[i];
PropertyInfo prop = current.GetType().GetProperty("id", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (prop != null)
{
    int id = (int)prop.GetValue(current, null);
}

有效。

I have a method that should return the ids from a List.
Usually I would use reflection for this task (I cannot use a generic method since the classes are usually POCOS that don't share an interface or a base class and I can't modify them). However, I thought about the new dynamic keyword and wanted to try this.

However my problem is that dataSource[index] returns an object. Well at runtime it is ensured that the object isself is on of my own classes and has a id property.
But I suppose because the method returns an object, I get a RumtineBinderException at runtime while accessing current.id

public List<int> GetItemIds()
{

    var result = new List<int>();
    var dataSource = GetDataSource(); // returns an List<Object>

    for (int i = 0; i <= dataSource.Count - 1; i++)
    {
        dynamic current = dataSource[i];
        int id = current.Id;  // throws RuntimeBinderException: Object has no definition for id
    }

    return result;
}

Is there a way to achive what I want or do I have to go back to reflection to get the id property?

Update:

current.GetType() returns object
current.GetType().GetProperties() returns a TargetInvocationException

My Pocos live in my main project (VB.net) but this method is in a class libary, maybe that is the cause. However:

object current = dataSource[i];
PropertyInfo prop = current.GetType().GetProperty("id", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (prop != null)
{
    int id = (int)prop.GetValue(current, null);
}

works.

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

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

发布评论

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

评论(2

迷鸟归林 2024-11-26 20:02:37

我相信您可能需要将“GetDataSource()”的返回类型定义为“List”。

当然,正如注释中所述,对象必须定义属性“id”。

I believe you may need to define the return type of "GetDataSource()" as "List<dynamic>".

Of course, as stated in the comments, the objects must have the property "id" defined.

情话难免假 2024-11-26 20:02:37

C# 区分大小写,包括使用动态关键字时。您的调用是 int id = current.Id; 但您谈论的属性是小写 id,并且您的反射调用看起来不区分大小写。即使跨程序集边界,动态关键字调用公共实例属性也应该没有问题,因为它表示找不到该方法,我最好的猜测是您需要使用 int id = current.id;

C# is case sensitive including when you use the dynamic keyword. your call is int id = current.Id; but you talk about the property being lowercase id, and your reflection call looks case insensitive. The dynamic keyword should have no problem calling public instance properties even across assembly boundaries, since it says the method is not found my best guess is that you need to be using int id = current.id;

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