Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to define static method

I'm facing a requirement to create a static method on my base class, but don't like that I have to declare type arguments, so I'm wondering if I'm going about this the right way.

Basically, I'm assigning delegates that I'll associate with properties on the class. I could easily put the method on the inherited classes, like so:

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 an 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 these would enable me to use AssignDel in an instantiated class:

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

But I have a requirement to make AssignDel static. This makes the extension way of doing it useless. It still works in InheritsFoo, but I really want to move this to the base class. If I try, the generics argument can't be inferred, and I have to change the usage of the method:

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

Is there a way out here, another way of doing this I haven't thought of?

EDIT: to address the issue in the comments about whether or not the extension method would/should work... I went to the url referenced by @Mark M. Turns out that if I write it as such...

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

That compiles (don't know if it will run, though). Still, don't think that qualifies as using a static method, since 'foo' is still considered an instance; A null instance, but an instance nonetheless.

like image 596
Random Avatar asked Nov 04 '22 23:11

Random


1 Answers

But I have a requirement to make AssignDel static. This makes the extension way of doing it useless. It still works in InheritsFoo, but I really want to move this to the base class. If I try, the generics argument can't be inferred, and I have to change the usage of the method:

This does not make a great deal of sense.

InheritsFoo.AssignDel is a static method.

You call said static method by doing InheritsFoo.AssignDel<InheritsFoo, string>(x => x.Property1, handler); it seems to meet your requirements.

I don't understand what is wrong with the second option you came up with. It does what you need to do, it is clear what is happening, is it really because you pass InheritsFoo and string instead of foo.AssignDel(x => x.Property1, handler);?

It seems you could simply do the following and achieve what you want.

   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) 
        {
        }
    }

I must be missing something because it would seem that you would use it InheritsFoo.AssignDel(x => x.Property1, handler); which is exactly what you want.

like image 184
Security Hound Avatar answered Nov 09 '22 05:11

Security Hound