Can I accept both the delegate type T and the expression <T> in the same parameter?

I am trying to write a helper class that represents fields on an object. The helper class must be able to both obtain the value of the field in this instance and return metadata about the underlying property, which it can obtain by reflection.

I would like the helper class to be created using the utility method, which is called as follows:

public IEnumerable<IFieldInfo<SomeType>> Fields {
  get {
    yield return FieldHelper.GetFieldInfo<SomeType>(t => t.SomeField);
    yield return FieldHelper.GetFieldInfo<SomeType>(t => t.SomeOtherField);
  }
}

Let's say the interface IFieldInfolooks something like this:

interface IFieldInfo<in T> {
  public string PropertyName {get;}
  public object GetValue(T obj);
}

Fields ; GetValue() , PropertyName ( /, ) . , Fields, , , , GetFieldInfo, ; , .

, GetFieldInfo? :

public static IFieldInfo<T> GetFieldInfo<T>(Func<T, object> getter);

GetValue() , getter(obj), .

, , :

public static IFieldInfo<T> GetFieldInfo<T>(Expression<Func<T, object>> getter)

, , GetValue() - Compile() , , , - , GetValue() , PropertyName.

GetFieldInfo, ?

+3
1

, . - , . :

  MyFunc("10m");

, , .

Compile() . , .

+1

Source: https://habr.com/ru/post/1762509/


All Articles