与一般的功能和QUOT;拥有财产X'QUOT;约束? [英] generic function with a "has property X" constraint?

查看:111
本文介绍了与一般的功能和QUOT;拥有财产X'QUOT;约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方,关闭时导出一个COM接口,我使用通过互操作我的C#.NET应用程序,源应用程序。这个COM接口出口多的对象,所有显示为System.Object的,直到我将它们转换成相应的接口类型。我要分配所有这些对象的属性。因此:

I have a third-party, closed source application that exports a COM interface, which I am using in my C#.NET application through Interop. This COM interface exports many objects that all show up as System.Object until I cast them to the appropriate interface type. I want to assign an property of all of these objects. Thus:

foreach (object x in BigComInterface.Chickens)
{
    (x as Chicken).attribute = value;
}
foreach (object x in BigComInterface.Ducks)
{
    (x as Duck).attribute = value;
}



但分配的财产很可能(对于那些无法避免的应用程序特定的原因)扔从中我想恢复例外,所以我真的希望周围的每个人一个try / catch。因此:

But assigning the property is likely (for application-specific reasons that are unavoidable) to throw Exceptions from which I want to recover, so I really want a try/catch around each one. Thus:

foreach (object x in BigComInterface.Chickens)
{
    try
    {
        (x as Chicken).attribute = value;
    }
    catch (Exception ex)
    {
        // handle...
    }
}
foreach (object x in BigComInterface.Ducks)
{
    try
    {
        (x as Duck).attribute = value;
    }
    catch (Exception ex)
    {
        // handle...
    }
}

显然,这将是这么多的清洁要做到这一点:

Obviously, it would be so much cleaner to do this:

foreach (object x in BigComInterface.Chickens)
{
    SetAttribute<Chicken>(x as Chicken, value);
}
foreach (object x in BigComInterface.Ducks)
{
    SetAttribute<Duck>(x as Duck, value);
}

void SetAttribute<T>(T x, System.Object value)
{
    try
    {
        x.attribute = value;
    }
    catch
    {
        // handle...
    }
}

看到这个问题?我的 X 的值可以是任何类型的,所以编译器无法解析的属性约的。鸡和鸭都没有在任何类型的继承树,他们不同意有属性约的接口。如果他们这么做,我可以把一个约束上的 T 的该接口​​。但由于类是封闭源代码,这是不可能的我。

See the problem? My x value can be of any type, so the compiler can't resolve .attribute. Chicken and Duck are not in any kind of inheritance tree and they do not share an interface that has .attribute. If they did, I could put a constraint for that interface on T. But since the class is closed-source, that's not possible for me.

我想要什么,在我的幻想,是一样的东西,需要的参数约束的具有属性约属性的不管它是否实现给定的接口。要机智,

What I want, in my fantasy, is something like a constraint requiring the argument to have the .attribute property regardless of whether it implements a given interface. To wit,

void SetAttribute<T>(T x, System.Object value) where T:hasproperty(attribute)

我不知道该怎么在这里做其他比剪切/粘贴这个小try / catch块。每个鸡,鸭,牛,羊,等等

I'm not sure what to do from here other than to cut/paste this little try/catch block for each of Chicken, Duck, Cow, Sheep, and so on.

我的问题是:什么是想调用的特定属性的这个问题一个很好的解决方法对象时实现该属性不能在编译时已知的接口?

My question is: What is a good workaround for this problem of wanting to invoke a specific property on an object when the interface that implements that property cannot be known at compile time?

推荐答案

不幸的是,这是目前非常棘手。在C#4,动态类型可能有助于颇有几分与此有关。 COM互操作是动态的真正闪光的地方之一。

Unfortunately, this is tricky currently. In C# 4, the dynamic type may help quite a bit with this. COM interop is one of the places that dynamic really shines.

然而,在此期间,允许唯一的选择你有任何类型的对象,有没有限制接口,将恢复使用反射。

However, in the meantime, the only option that allows you to have any type of object, with no restrictions on interfaces, would be to revert to using reflection.

您可以使用反射来找到属性属性,并设置它在运行时的值。

You can use reflection to find the "attribute" property, and set it's value at runtime.

这篇关于与一般的功能和QUOT;拥有财产X'QUOT;约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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