lundi 5 décembre 2016

correct signature to use in getmethod on class with multiple overloaded generic methods requiring delegate argument

What should the method signature look like to reflection, I'm trying to invoke the equivalent of this

DynamicPropertyManager<ThreeColumns>.CreateProperty<ThreeColumns, string>(
     "Four",
     t => "Four",
     null
  ));

on this class found here http://ift.tt/2gvVAhl

  1. But I'm trying to do it using reflection. What I'm struggling with the most is getting the correct method overload.

  2. I have to be honest though I'm also not totally sure how to supply the correct argument for the lambda bit through reflection either.

I was going to try this in part but don't know what the func bit would look like when doing MakeGenericMethod

Func<string> funcArg = () => { return "Four"; };

object[] args = { fieldOrPropertyName , funcArg, null };

The class contents from the link above are included for reference.

public class DynamicPropertyManager<TTarget> : IDisposable
{
    private readonly DynamicTypeDescriptionProvider provider;
    private readonly TTarget target;

    public DynamicPropertyManager()
    {
        Type type = typeof(TTarget);

        provider = new DynamicTypeDescriptionProvider(type);
        TypeDescriptor.AddProvider(provider, type);
    }

    public DynamicPropertyManager(TTarget target)
    {
        this.target = target;

        provider = new DynamicTypeDescriptionProvider(typeof(TTarget));
        TypeDescriptor.AddProvider(provider, target);
    }

    public IList<PropertyDescriptor> Properties
    {
        get { return provider.Properties; }
    }

    public void Dispose()
    {
        if (ReferenceEquals(target, null))
        {
            TypeDescriptor.RemoveProvider(provider, typeof(TTarget));
        }
        else
        {
            TypeDescriptor.RemoveProvider(provider, target);
        }
    }

    public static DynamicPropertyDescriptor<TTargetType, TPropertyType>
       CreateProperty<TTargetType, TPropertyType>(
           string displayName,
           Func<TTargetType, TPropertyType> getter,
           Action<TTargetType, TPropertyType> setter,
           Attribute[] attributes)
    {
        return new DynamicPropertyDescriptor<TTargetType, TPropertyType>(
           displayName, getter, setter, attributes);
    }

    public static DynamicPropertyDescriptor<TTargetType, TPropertyType>
       CreateProperty1<TTargetType, TPropertyType>(
          string displayName,
          Func<TTargetType, TPropertyType> getHandler,
          Attribute[] attributes)
    {
        return new DynamicPropertyDescriptor<TTargetType, TPropertyType>(
           displayName, getHandler, (t, p) => { }, attributes);
    }

    public static DynamicPropertyDescriptor<TTargetType, TPropertyType>
       CreateProperty<TTargetType, TPropertyType>(
          string displayName,
          Func<TTargetType, TPropertyType> getHandler,
          Attribute[] attributes)
    {
        return new DynamicPropertyDescriptor<TTargetType, TPropertyType>(
           displayName, getHandler, (t, p) => { }, attributes);
    }
}





Aucun commentaire:

Enregistrer un commentaire