简体   繁体   中英

How to create custom attribute with Func<> parameter

I have a class that helps me read data from an MS SQL database into a list of objects. For the most part it's pretty straightforward; I can assume the property name of the class matches the column name of the table and just assign it accordingly, but sometimes I need to be able to transform data.

I have created a custom attribute to put on my class properties:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class TransformDataAttribute : Attribute
{
    public Func<object, string, object> TransformThisData { get; set; }
}

Now, let's say I want to create the Func on the fly, like this:

    [TransformData(TransformThisData = new Func<object, string, object>((v, p) => "My name is " + v.ToString()))]
    public string Name { get; set; }

The error that I am seeing is 'TransformThisData' is not a valid named attribute argument because it is not a valid attribute parameter type.

What is the best way to accomplish Func as a property attribute?

Well, here's the best I have been able to come up with.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class TransformDataAttribute : Attribute
{
    public string TransformDataClass { get; set; }
    // This method must contain these parameters: (object value, PropertyInfo pi)
    public string TransformDataMethod { get; set; }
}

I put it on the class property, like so...

  public class Tracker
  {
    [TransformData(TransformDataClass = "CompanyTracker.DataTransformation", TransformDataMethod = "FunkyData")]
    public string FunkyData { get; set; }
  }

I can have a single data transformation class with different methods of transformation:

public class DataTransformation
{
    public object FunkyData(object value, PropertyInfo pi)
    {
        // if data is this, return that, blah, blah
        return value;
    }
}

A static utility method for interpreting the three parameters:

    public static object CallThisMethod(string className, string methodName, object[] parms)
    {
        Type type = Type.GetType(className);
        MethodInfo theMethod = type.GetMethod(methodName);
        object classInstance = Activator.CreateInstance(type);

        return theMethod.Invoke(classInstance, parms);
    }

...and then in my ADO Helper code, when it comes to assigning values to properties:

        TransformDataAttribute attr = Utility.GetPropertyAttribute<TransformDataAttribute>(pi);
        if (attr != null)
        {
            object[] parms = new object[] { value, pi };
            value = Utility.CallThisMethod(attr.TransformDataClass, attr.TransformDataMethod, parms);
        }
        pi.SetValue(t, value, null);

It works. I hate to depend on Reflection of embedded strings for classes and methods, and it just doesn't seem like good design, but sometimes you just have to get things done. If anyone has a more elegant way to do this I'd be glad to hear about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM