获取一个字段使用反射在C#中的属性 [英] Getting the attributes of a field using reflection in C#

查看:154
本文介绍了获取一个字段使用反射在C#中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的,从这样一个对象的字段抽取的方法:

 私有静态字符串GetHTMLStatic(Ref对象_对象,参考名单,LT;字符串> ExludeFields)
{
    键入的objectType = objectX.GetType();
    字段信息[]字段信息= objectType.GetFields();    的foreach(在字段信息字段信息字段)
    {
        如果(!ExludeFields.Contains(field.Name))
        {
            DisplayOutput + = GetHTMLAttributes(场);
        }
    }    返回DisplayOutput;
}

在我的类中的每个领域也有着它自己的属性,在这种情况下我的属性称为HTMLAttributes。里面的foreach循环,我试图让每个字段的属性和它们各自的值。目前,它看起来像这样:

 私有静态字符串GetHTMLAttributes(字段信息字段)
{
    字符串AttributeOutput =的String.Empty;    HTMLAttributes [] = htmlAttributes field.GetCustomAttributes(typeof运算(HTMLAttributes),FALSE);    的foreach(HTMLAttributes FA在htmlAttributes)
    {
        //做的东西在这里领域的属性。
    }    返回AttributeOutput;
}

我的属性类看起来是这样的:

  [AttributeUsage(AttributeTargets.Field,
                的AllowMultiple = TRUE)]
公共类HTMLAttributes:System.Attribute
{
    公共字符串的字段类型;
    公共字符串inputType下;    公共HTMLAttributes(字符串FTYPE,串ITYPE)
    {
        字段类型= fType.ToString();
        inputType下= iType.ToString();
    }
}

这似乎是合乎逻辑,但它不会编译,我在GetHTMLAttributes一个红色的波浪线()方法下的:

  field.GetCustomAttributes(typeof运算(HTMLAttributes),FALSE);

我试图提取属性的是像这样使用另一个类领域:

  [HTMLAttributes(输入,文本)]
公共字符串客户名称;

从我的理解(或缺乏)这应该工作?请展开我的脑海里的开发伙伴们!

*编辑器,编译器错误


  

无法隐式转换类型
  '对象[]'到'data.HTMLAttributes []'。
  显式转换存在(是你
  缺少强制转换?)


我已经试过铸造这样的:

 (HTMLAttributes)field.GetCustomAttributes(typeof运算(HTMLAttributes),FALSE);

但是,这也不起作用,我得到这个编译器错误:


  

无法将类型'对象[]来
  data.HTMLAttributes



解决方案

GetCustomAttributes 方法返回一个 [对象] ,不是 HTMLAttributes [] 。它返回其原因 [对象] 是,它一直在那里自1.0,.NET之前看泛型天日。

您应该手动返回值投每个项目 HTMLAttributes

要解决您的code,您只需将线更改为:

  [对象] htmlAttributes = field.GetCustomAttributes(typeof运算(HTMLAttributes),FALSE);

的foreach 会照顾剧组的你。

更新:

不应转换返回数组 HTMLAttributes [] 。返回值是不是 HTMLAttributes [] 。这是一个 [对象] 包含类型的元素 HTMLAttributes 。如果你想有一个 HTMLAttribute [] 类型的对象(你不要在这个特定的code片断需要,的foreach 就足够了),你应该阵列的每个元素分别投给 HTMLAttribute ;也许使用LINQ:

  HTMLAttributes [] = htmlAttributes&returnValue.Cast LT; HTMLAttributes>()ToArray的()。

I wrote a method that extracts fields from an object like this:

private static string GetHTMLStatic(ref Object objectX, ref List<string> ExludeFields)
{
    Type objectType = objectX.GetType();
    FieldInfo[] fieldInfo = objectType.GetFields();

    foreach (FieldInfo field in fieldInfo)
    {
        if(!ExludeFields.Contains(field.Name))
        {
            DisplayOutput += GetHTMLAttributes(field);
        }                
    }

    return DisplayOutput;
}

Each field in my class also has it's own attributes, in this case my attribute is called HTMLAttributes. Inside the foreach loop I'm trying to get the attributes for each field and their respective values. It currently looks like this:

private static string GetHTMLAttributes(FieldInfo field)
{
    string AttributeOutput = string.Empty;

    HTMLAttributes[] htmlAttributes = field.GetCustomAttributes(typeof(HTMLAttributes), false);

    foreach (HTMLAttributes fa in htmlAttributes)
    {
        //Do stuff with the field's attributes here.
    }

    return AttributeOutput;
}

My attributes class looks like this:

[AttributeUsage(AttributeTargets.Field,
                AllowMultiple = true)]
public class HTMLAttributes : System.Attribute
{
    public string fieldType;
    public string inputType;

    public HTMLAttributes(string fType, string iType)
    {
        fieldType = fType.ToString();
        inputType = iType.ToString();
    }
}

This seems logical but it won't compile, I have a red squiggly line in the GetHTMLAttributes() method under:

field.GetCustomAttributes(typeof(HTMLAttributes), false);

The field I'm trying to extract the attributes from is in another class used like this:

[HTMLAttributes("input", "text")]
public string CustomerName;

From my understanding (or lack thereof) this should work? Please expand my mind fellow developers!

*Edit, compiler error:

Cannot implicitly convert type 'object[]' to 'data.HTMLAttributes[]'. An explicit conversion exists (are you missing a cast?)

I have tried casting it like this:

(HTMLAttributes)field.GetCustomAttributes(typeof(HTMLAttributes), false);

But that also doesn't work, I get this compiler error:

Cannot convert type 'object[]' to 'data.HTMLAttributes'

解决方案

GetCustomAttributes method returns an object[], not HTMLAttributes[]. The reason it returns object[] is that it has been there since 1.0, before .NET generics see the light of day.

You should manually cast each item in the return value to HTMLAttributes.

To fix your code, you simply need to change the line to:

object[] htmlAttributes = field.GetCustomAttributes(typeof(HTMLAttributes), false);

foreach will take care of the cast for you.

Update:

You shouldn't cast the returned array to HTMLAttributes[]. The return value is not HTMLAttributes[]. It's an object[] containing elements of type HTMLAttributes. If you want a HTMLAttribute[] typed object (which you don't need in this specific code snippet, foreach would suffice), you should cast each element of array individually to HTMLAttribute; perhaps using LINQ:

HTMLAttributes[] htmlAttributes = returnValue.Cast<HTMLAttributes>().ToArray();

这篇关于获取一个字段使用反射在C#中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆