How can a dynamic be used as a generic?

前端 未结 6 435
南旧
南旧 2021-01-02 06:08

How can I use a dynamic as a generic?

This

var x = something not strongly typed;
callFunction();

and this

         


        
6条回答
  •  被撕碎了的回忆
    2021-01-02 06:56

    The quickest way to make this work is to make your anonymous type a real type.

    So instead of

    var x = new { Value = "somevalue", Text = "sometext" };
    

    You need to do

    class MyClass
    {
        string Text, Value;
    }
    ....
    var x = new MyClass() { Value = "somevalue", Text = "sometext" };
    //this should work now
    callFunction(x);
    

提交回复
热议问题