反射 - 如何获取参数指定索引处的值

发布于 2024-12-20 10:32:44 字数 1596 浏览 3 评论 0原文

我正在尝试使用反射获取属性的指定索引的值。

此答案适用于List<> 类型的标准属性例如,但在我的例子中,我尝试使用的集合具有不同的格式:

public class NumberCollection : List<int>
{
    public NumberCollection()
    {
        nums = new List<int>();
        nums.Add(10);
    }

    public new int this[int i]
    {
        get { return (int) nums[i]; }
    }

    private List<int> nums;

}

public class TestClass
{
    public NumberCollection Values { get; private set; }

    public TestClass()
    {
        Values = new NumberCollection();
        Values.Add(23);
    }
}


class Program
{
    static void Main(string[] args)
    {
        TestClass tc = new TestClass();

        PropertyInfo pi1 = tc.GetType().GetProperty("Values");
        Object collection = pi1.GetValue(tc, null);

        // note that there's no checking here that the object really
        // is a collection and thus really has the attribute
        String indexerName = ((DefaultMemberAttribute)collection.GetType()
            .GetCustomAttributes(typeof(DefaultMemberAttribute),
                true)[0]).MemberName;
        // Code will ERROR on the next line...
        PropertyInfo pi2 = collection.GetType().GetProperty(indexerName);
        Object value = pi2.GetValue(collection, new Object[] { 0 });

        Console.Out.WriteLine("tc.Values[0]: " + value);
        Console.In.ReadLine();
    }
}

此代码给出 AmbigouslyMatchException(“发现不明确的匹配。”)。我知道我的收藏类有点做作,但有人可以帮忙吗?

I'm trying to get the value of a specified index of a property using reflection.

This answer works for standard properties that are of type List<> for example, but in my case, the collection I am trying to work with is of a different format:

public class NumberCollection : List<int>
{
    public NumberCollection()
    {
        nums = new List<int>();
        nums.Add(10);
    }

    public new int this[int i]
    {
        get { return (int) nums[i]; }
    }

    private List<int> nums;

}

public class TestClass
{
    public NumberCollection Values { get; private set; }

    public TestClass()
    {
        Values = new NumberCollection();
        Values.Add(23);
    }
}


class Program
{
    static void Main(string[] args)
    {
        TestClass tc = new TestClass();

        PropertyInfo pi1 = tc.GetType().GetProperty("Values");
        Object collection = pi1.GetValue(tc, null);

        // note that there's no checking here that the object really
        // is a collection and thus really has the attribute
        String indexerName = ((DefaultMemberAttribute)collection.GetType()
            .GetCustomAttributes(typeof(DefaultMemberAttribute),
                true)[0]).MemberName;
        // Code will ERROR on the next line...
        PropertyInfo pi2 = collection.GetType().GetProperty(indexerName);
        Object value = pi2.GetValue(collection, new Object[] { 0 });

        Console.Out.WriteLine("tc.Values[0]: " + value);
        Console.In.ReadLine();
    }
}

This code gives an AmbiguousMatchException ("Ambiguous match found."). I know my collection class is somewhat contrived, but can anyone help with this?

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

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

发布评论

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

评论(2

我是有多爱你 2024-12-27 10:32:44

一种选择是根据需要

var prop = Type.GetProperties()
               .Where(prop => prop.DeclaringType == collection.GetType())
               .First();

Collection.GetType() 更改为另一种类型。但基本上:循环属性而不是使用 Type.GetProperty 。

One option is to use

var prop = Type.GetProperties()
               .Where(prop => prop.DeclaringType == collection.GetType())
               .First();

Change Collection.GetType() to another type if you want. But basically: loop over the properties instead of using Type.GetProperty.

别再吹冷风 2024-12-27 10:32:44

如果您要查找所有默认成员,可以询问 Type.GetDefaultMembers(),然后检查成员以找到您要查找的成员。

或者,如果您知道索引器的数据类型,则可以调用 GetPropertyInfo 与类型数组说明符。

If you are looking for all of the default members, you can ask for Type.GetDefaultMembers(), then examine the members to find the one that you are looking for.

Alternatively, if you know the data type of the indexer, you can call GetPropertyInfo with the type array specifier.

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