Retrieving field attributes using reflection in C #

I wrote a method that retrieves 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 its own attributes, in this case my attribute is called HTMLAttributes. Inside the foreach loop, I am trying to get the attributes for each field and their corresponding 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 attributes here.
    }

    return AttributeOutput;
}

      

My attribute 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();
    }
}

      

It 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 in 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 developer thoughts!

* Edit, compiler error :

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

I tried to do it like this:

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

      

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

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

+2


source to share


1 answer


GetCustomAttributes

returns the value object[]

, not HTMLAttributes[]

. The reason it returns object[]

is because it has been around since 1.0 before .NET generics see the light of day.

You have to manually cast each item in the return value before HTMLAttributes

.

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

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

      



foreach

will take care of you.

Update:

You shouldn't be passing the returned array to HTMLAttributes[]

. The return value is not equal HTMLAttributes[]

. It is a object[]

containing elements of type HTMLAttributes

. If you want a typed object HTMLAttribute[]

(which you don't need in this particular piece of code, foreach

), you must allocate each element of the array individually before HTMLAttribute

; perhaps using LINQ:

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

      

+12


source







All Articles