简体   繁体   English

C#动态和扩展方法的解决方案?

[英]c# dynamic and extension method solution?

I have a method in Base class which calls ( by reflection to another method). 我在Base class有一个方法调用(通过反射到另一个方法)。

  type.InvokeMember(context.Request["MethodName"],
                    System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, 
                    null, 
                    this, 
                    new object[] { context, Jobj });   //  jObj is dynamic

jObj parameter type is dynamic ( can't change this type). jObj参数类型是dynamic (无法更改此类型)。

if the MethodName string value is : " getFinanceDetails " so that method is called.. 如果MethodName字符串值为:“ getFinanceDetails ”,则调用该方法。

void getFinanceDetails(object contextObj, dynamic obj)
{

  //Here I need to do obj["Inv_num"].ToString().Decrpyt()   ( my extension method).
  //but it goes Bang cause I cant use extension method for dynamic.
  //But I cant also send it decrypted from base cause not all values are encrpyrted.

}

However - I did solve it by using (inside the method): 但是-我确实通过使用(在方法内部)解决了它:

((object) obj["Inv_num"]).ToString().Decrypt();

But I dont want to cast every time to object , just to enable extension method. 但是我不想每次都强制转换为object,只是为了启用扩展方法。

Is there anything I can do with the param type sending to fix it ? 我可以用param类型发送来解决它吗?

my desire : 我的愿望 :

I want to be able to do : obj.ToString().Decrpyt() obj["Inv_num"].ToString().Decrpyt() 我希望能够做到: obj.ToString().Decrpyt() obj["Inv_num"].ToString().Decrpyt()

edit 编辑

   public static string Decrypt(this string obj)
        {
            Func<string, string> Decrypt = Encryptions.GetDecryptedCode;
            return Decrypt(obj);

        }
  • obj ( in this case is IDictionary<string , object> ) . obj( 在这种情况下IDictionary<string , object> )。 so I should be able to read properties. 所以我应该能够读取属性。 (inv_num in this sample. (此示例中的inv_num。

Probably not exactly the syntax you were looking for but you could call the extension method as a simple static method on the dynamic object: 可能不完全是您要查找的语法,但是您可以将扩展方法作为动态对象上的简单静态方法调用:

void getFinanceDetails(object contextObj, dynamic obj)
{
    var decryptedValue = MyExtensions.Decrypt(obj);
}

This obviously assumes that at runtime obj is of the correct type that your extension method operates on. 显然,这假定obj在运行时是扩展方法所针对的正确类型。 In your question you have shown some obj["Inv_num"] as if obj was a complex type with a property called Inv_num which is of type string. 在您的问题中,您显示了一些obj["Inv_num"] ,就好像obj是一个复杂的类型,具有一个名为Inv_num的字符串类型的属性。 So you might need to adjust the call on the proper type: 因此,您可能需要将呼叫调整为适当的类型:

var decryptedValue = MyExtensions.Decrypt(obj["Inv_num"]);

Assuming obj["Inv_num"].ToString() already returns the right value, you could easily do it in two steps: 假设obj["Inv_num"].ToString()已经返回正确的值,则可以通过两步轻松地完成操作:

string text = obj["Inv_num"].ToString();
var decrypted = text.Decrypt();

To be honest, it's not clear why getFinanceDetails (which should be changed to follow .NET naming conventions) can't be written as: 老实说,尚不清楚为什么不能将getFinanceDetails (应更改为遵循.NET命名约定)的形式为:

void getFinanceDetails(object contextObj, IDictionary<string, object> obj)
{
    var decrypted = obj["Inv_num"].ToString().Decrypt();
}

Do you ever need to call it with something that doesn't implement IDictionary<string, object> ? 您是否需要使用实现IDictionary<string, object>东西来调用它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM