I was requested to create a static method in my base class, but I do not like that I have to declare type arguments, so I wonder if I will do it right.
Basically, I appoint delegates to associate them with properties. I could easily put the method in inherited classes, for example:
public class Foo
{
public string Property1 { get; set; }
}
public class InheritsFoo : Foo
{
public void AssignDel<TVal>(
Expression<Func<InheritsFoo, TVal>> expr,
Action<InheritsFoo, TVal> action)
{
}
}
Or, in the extension class, I could do this:
public static void AssignDel<T, TVal>(
this T source,
Expression<T, TVal>> expression,
Action<T, TVal> action)
where T : Foo
{
}
Both of them will allow me to use AssignDelin an instance of the class:
var foo = new InheritsFoo();
foo.AssignDel(x => x.Property1, handler);
But I have a requirement to make it static AssignDel. This makes the extension a way to make it useless. It still works in InheritsFoo, but I really want to move this to the base class. If I try, the generics argument cannot be inferred, and I have to change the use of the method:
InheritsFoo.AssignDel<InheritsFoo, string>(x => x.Property1, handler);
, , ?
EDIT: , / ... URL-, @Mark M. , ...
InheritsFoo foo = null;
foo.AssignDel(x => x.Property1, handler);
( , , ). , , , "foo" - ; , .