Best way to define a static method

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" - ; , .

+5
3

, , .

public class Foo  
{    
   public string Property1 { get; set; } 
} 

public class Foo<T> : Foo
{
   public static void AssignDel<TVal>(Expression<Func<T, TVal>> expr, Action<T, TVal> action)
   {   }
}

public class InheritsFoo : Foo<InheritsFoo>
{     } 

InheritsFoo , .

0

AssignDel. . - InheritsFoo, . , , :

.

InheritsFoo.AssignDel .

, InheritsFoo.AssignDel<InheritsFoo, string>(x => x.Property1, handler);, , .

, , . , , , , , InheritsFoo string foo.AssignDel(x => x.Property1, handler);?

, , .

   public class Foo 
    {
       public string Property1 { get; set; }
    }

    public class InheritsFoo : Foo 
    {
        public static void AssignDel<TVal>(
            Expression<Func<InheritsFoo, TVal>> expr, 
            Action<InheritsFoo, TVal> action) 
        {
        }
    }

, , - , , , InheritsFoo.AssignDel(x => x.Property1, handler);, .

+1

.

Suppose you don't need to use it in an extension method, this should work:

InheritsFoo.AssignDel(x => x.Property1, handler);

In the same way, the compiler will determine the type parameters for the form of the extension method, this will be for the old-fashioned static way.

If you need a method with two type parameters, you can create a general class for it:

public class Foo<T> where T : Foo {

    public void AssignDel<TVal>( Expression<Func<T, TVal>> expr, Action<T, TVal> action) 
    {
         //...
    }
}

In this case, you can do:

Foo<InheritFoo>.AssignDel(x => x.PropertyFromInheritFoo, handler); 

As you can see, you only need to declare one type parameter, the other is output.

Hope this helps

+1
source

All Articles