Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以更通用的方式计算自定义模型属性_C#_Winforms_Generics_System.componentmodel - Fatal编程技术网

C# 以更通用的方式计算自定义模型属性

C# 以更通用的方式计算自定义模型属性,c#,winforms,generics,system.componentmodel,C#,Winforms,Generics,System.componentmodel,我有一个如下的数据模型。它的一个属性被自定义属性“Mergable”修饰: 以下是“Mergable”的定义: 我使用AttributeEvaluator类来评估属性: public class AttributeEvaluator { public bool Evaluate(object instance, string propertyName) { AttributeCollection attributes = TypeDescriptor.GetPro

我有一个如下的数据模型。它的一个属性被自定义属性“Mergable”修饰:

以下是“Mergable”的定义:

我使用AttributeEvaluator类来评估属性:

public class AttributeEvaluator
{
    public bool Evaluate(object instance, string propertyName)
    {
        AttributeCollection attributes = TypeDescriptor.GetProperties(instance)[propertyName].Attributes;
        foreach (var a in attributes)
        {
            if (a.GetType() == typeof(MergableAttribute))
            {
                return ((MergableAttribute)a).Mergable;
            }
        }
        return false;
    }
}
我可以从如下表格中获取属性值:

private void btnAdd_Click(object sender, EventArgs e)
    {

        bool x = new AttributeEvaluator().Evaluate(new Example(), "ExampleId");
    }
我想知道怎样才能使它更通用。理想的情况是使用单个类来评估所有自定义属性。我可以将属性类型传递给Evaluate方法,但不能将其转换为适当的类型以获得“Mergable”值

公共类属性评估器
{
public bool Evaluate(对象实例、字符串propertyName、类型attributeType)
{
AttributeCollection attributes=TypeDescriptor.GetProperties(实例)[propertyName].attributes;
foreach(属性中的var a)
{
if(a.GetType()==attributeType)
{

return((MergableAttribute)a).Mergable;//使用反射获取属性值。
attributeType.GetProperty(“可合并”).GetValue(a)
或类似的内容。您需要参数化属性属性的名称。是否确实只希望能够获取碰巧是布尔值的属性属性?为什么属性上有布尔值?为什么不直接说属性上有属性,它是可合并的,如果属性不存在,那么它就不是可合并的。然后你就可以检查属性的存在性了。真不敢相信它这么容易:)现在布尔值将是fine@KMoussa,谢谢,你的建议也是一种方法。也许更好。我以[Browsable]属性为例,这样编码它如何以类似的方式[Browsable]将其用于dataGridView是否使用?我的意思是,如果列(属性)是可合并的,则在网格中合并此列的单元格。我有合并的代码,但它对列编号有效。
public class AttributeEvaluator
{
    public bool Evaluate(object instance, string propertyName)
    {
        AttributeCollection attributes = TypeDescriptor.GetProperties(instance)[propertyName].Attributes;
        foreach (var a in attributes)
        {
            if (a.GetType() == typeof(MergableAttribute))
            {
                return ((MergableAttribute)a).Mergable;
            }
        }
        return false;
    }
}
private void btnAdd_Click(object sender, EventArgs e)
    {

        bool x = new AttributeEvaluator().Evaluate(new Example(), "ExampleId");
    }
public class AttributeEvaluator
{
    public bool Evaluate(object instance, string propertyName, Type attributeType)
    {
        AttributeCollection attributes = TypeDescriptor.GetProperties(instance)[propertyName].Attributes;
        foreach (var a in attributes)
        {
            if (a.GetType() == attributeType)
            {
                return ((MergableAttribute)a).Mergable; //<-- how to cast a to appopriate type?
            }
        }
        return false;
    }
}