Is double submit in C # 4.0 a dynamic keyword?

I realize this has been asked before, but I haven't found a clear agreement on the best solution.

Is using a dynamic (like the one below) the best way to do this? I think it's best to avoid dynamic whenever possible to help catch problems at compile time.

(class A1 and class A2 implement interface A and also for B)

 public static void Foo(InterfaceA a, InterfaceB b) 
 { 
    Foo((dynamic)a, (dynamic)b); 
 }

static void Foo(classA1 a, classB1 b) {  //some code }
static void Foo(classA2 a, classB2 b) {  //some code }
static void Foo(classA1 a, classB2 b) {  //some code }
static void Foo(classA2 a, classB1 b) {  //some code }

      

Or similar ...

public static void Foo(InterfaceA a, InterfaceB b) 
 { 
    ((dynamic) a).Foo(b); 
 }

public classA1
{
     void Foo(classB1 b) {  //some code }
}
//repeated for other cases    

      

+3


source to share


4 answers


Is using a dynamic (like the one below) the best way to do this?

Well, that's the way to do it - as long as the runtime types will always end up with something to be satisfied with overload resolution.

You might want to set the backstop method



static void Foo(object x, object y)

      

in case none of the methods are applicable (for example, a

is an implementation other than ClassA1 / ClassA2). It won't help you if both are zero, mind you ...

I usually try to reverse engineer so that this isn't required, but it's hard to find a better solution without additional context.

+3


source


Are 'classA1' etc. implementations InterfaceA

? If so, why not just declare the function Foo

as a decision InterfaceA

and InterfaceB

to include them in the specific implementation, the expected function? For example.

static void Foo(InterfaceA a, InterfaceB b) {
    classA1 c1 = a as classA1;
    classB1 b1 = b as classB1;
    // ... etc
}

      



Dynamic is not intended to be used in this way.

0


source


C # is traditionally a statically typed language. The dynamic keyword adds dynamic typing to the language . The usual advice is to use "dynamic" sparingly . There might be a case where you need it.

Generics won't cut it as this won't compile:

    private void button1_Click(object sender, EventArgs e)
    {
        Foo(new classA1(), new classB2());
    }

    static void Foo<T, T1>(T a, T1 b) where T: InterfaceA
        where T1: InterfaceB
    {
        Foo2(a, b);
    }

    static void Foo2(classA1 a, classB1 b) { }
    static void Foo2(classA2 a, classB2 b) { }
    static void Foo2(classA1 a, classB2 b) { }
    static void Foo2(classA2 a, classB1 b) { }

    interface InterfaceA { }
    interface InterfaceB { }

    class classA1 : InterfaceA { }
    class classA2 : InterfaceA { }

    class classB1 : InterfaceB { }
    class classB2 : InterfaceB { }

      

0


source


You can do something AWFUL with reflection - but I'm pretty sure it's no better than dynamic:

void Main()
{
    var a = "hello";//5;
    var b = "hello"; 

    var type1 = a.GetType();
    var type2 = b.GetType();

    var t = typeof(FooClass);

    var methods = t.GetMethods();

    foreach(var method in methods)
    {
        var parameters = method.GetParameters();

        if(parameters.Length == 2)
        {
            if(parameters[0].ParameterType == type1 
               && parameters[1].ParameterType == type2)
            {
                method.Invoke(this, new object[]{ a, b });
            }
        }
    }
}

public static class FooClass
{
    public static void Foo(int i, string s)
    {
        "Foo1".Dump();
    }

    public static void Foo(string s, string s2)
    {
        "Foo2".Dump();
    }
}

      

0


source







All Articles