【问题标题】:Collection of Property=>Value?属性集合=>值?
【发布时间】:2010-07-15 18:54:38
【问题描述】:

我想在 C# 中创建一个键值对集合,其中键是 ASP.net 控件的属性(例如 ID),值是该属性的值。我想这样做,以便以后可以遍历集合并查看给定控件是否具有我的集合中的属性(并且控件中的属性值与我的集合中定义的值匹配)。关于执行此操作的最佳方法的任何建议?感谢您的帮助。

伪代码示例:

Properties[] = new Properties[] {new Property(){name="ID",value="TestControl1"}, new Property(){name = "Text",value="Type text here"}}

private bool controlContainsProperties(Control control){
    foreach(Property Property in Properties[])
    {
        if((control does not contain property) || (control property value != Property.Value))
        return false;
    }
return true;
}

【问题讨论】:

    标签: asp.net c#-3.0


    【解决方案1】:

    没有对此进行测试,但这是我的尝试:

        public bool HasProperty( object target, IDictionary<string, object> values )
        {
            var targetType = target.GetType();
            return values.All( kvp =>
                {
                    var property = targetType.GetProperty( kvp.Key );
                    if ( property != null )
                    {
                        var value = property.GetValue( target, null );
                        if ( value != null )
                            return value.Equals( kvp.Value );
                    }
                    return false;
                } );
        }
    

    【讨论】:

      【解决方案2】:

      我的第一个想法是使用“标签”属性,但后来我意识到 APS.NET 控件中没有标签。但是,有一个关于标签的回答 question

      在同一个线程中,有一个带有“属性”属性映射的解决方案 - 看起来很有希望。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-02
        • 2018-08-15
        • 2014-02-08
        • 1970-01-01
        • 2013-02-07
        • 2014-03-01
        • 2017-04-29
        • 2023-03-26
        相关资源
        最近更新 更多