使用C#4进行动态对象的鸭型测试 [英] Duck type testing with C# 4 for dynamic objects

查看:242
本文介绍了使用C#4进行动态对象的鸭型测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#中使用动态对象有一个简单的鸭类型的例子。对我来说,一个动态对象应该有HasValue / HasProperty / HasMethod方法与一个单一的字符串参数的值,属性或方法的名称,试图运行它之前。我试图避免尝试/ catch块,如果可能的话,更深的反射。它似乎是一个常见的做法是在动态语言(JS,Ruby,Python等)中测试属性/方法,然后再使用它,然后回到默认值,或抛出受控异常中。下面的例子基本上是我想要完成的。



如果上面描述的方法不存在,有没有人为动态的预制扩展方法将这样做? / p>



示例:在JavaScript中,我可以很容易地测试对象上的方法。

  // JavaScript 
function quack(duck){
if(duck && typeof duck.quack ===function){
return duck.quack();
}
return null; //无返回,不是鸭子
}



在C#中执行相同的操作吗?

  // C#4 
动态Quack(动态鸭)
{
//如何测试鸭不是null,
//并且有一个quack方法?

//如果不嘎嘎,返回null
}


解决方案

使用System.Linq尝试此操作:

  
using System.Reflection;
// ...
public dynamic Quack(dynamic duck,int i)
{
Object obj = duck as Object;

if(duck!= null)
{
//检查对象是否有方法Quack()
MethodInfo method = obj.GetType()GetMethods中。
FirstOrDefault(x => x.Name ==Quack);

//如果是
if(method!= null)
{

//调用并返回值
return method.Invoke ((object)duck,null);
}
}

return null;
}

或者(仅使用动态):

  public static dynamic Quack(动态鸭)
{
try
{
//调用并返回值
return duck.Quack();
}
//如果方法调用失败则抛出
catch(RuntimeBinderException)
{
return null;
}
}


I'm wanting to have a simple duck typing example in C# using dynamic objects. It would seem to me, that a dynamic object should have HasValue/HasProperty/HasMethod methods with a single string parameter for the name of the value, property, or method you are looking for before trying to run against it. I'm trying to avoid try/catch blocks, and deeper reflection if possible. It just seems to be a common practice for duck typing in dynamic languages (JS, Ruby, Python etc.) that is to test for a property/method before trying to use it, then falling back to a default, or throwing a controlled exception. The example below is basically what I want to accomplish.

If the methods described above don't exist, does anyone have premade extension methods for dynamic that will do this?


Example: In JavaScript I can test for a method on an object fairly easily.

//JavaScript
function quack(duck) {
  if (duck && typeof duck.quack === "function") {
    return duck.quack();
  }
  return null; //nothing to return, not a duck
}


How would I do the same in C#?

//C# 4
dynamic Quack(dynamic duck)
{
  //how do I test that the duck is not null, 
  //and has a quack method?

  //if it doesn't quack, return null
}

解决方案

Try this:

    using System.Linq;
    using System.Reflection;
    //...
    public dynamic Quack(dynamic duck, int i)
    {
        Object obj = duck as Object;

        if (duck != null)
        {
            //check if object has method Quack()
            MethodInfo method = obj.GetType().GetMethods().
                            FirstOrDefault(x => x.Name == "Quack");

            //if yes
            if (method != null)
            {

                //invoke and return value
                return method.Invoke((object)duck, null);
            }
        }

        return null;
    }

Or this (uses only dynamic):

    public static dynamic Quack(dynamic duck)
    {
        try
        {
            //invoke and return value
            return duck.Quack();
        }
        //thrown if method call failed
        catch (RuntimeBinderException)
        {
            return null;
        }        
    }

这篇关于使用C#4进行动态对象的鸭型测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆