Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 避免外部代码中的属性歧义_C#_.net_.net 4.5 - Fatal编程技术网

C# 避免外部代码中的属性歧义

C# 避免外部代码中的属性歧义,c#,.net,.net-4.5,C#,.net,.net 4.5,我正在使用一个外部库(我无法更改),并且在尝试设置属性时看到一个不明确的引用 这是一个示例(不是实际的库或属性名称) 外部代码: namespace ExternalLibrary1.Area1 { public interface Interface0: Interface1, Interface2 { } public interface Interface1 { double Item { get; set; } }

我正在使用一个外部库(我无法更改),并且在尝试设置属性时看到一个不明确的引用

这是一个示例(不是实际的库或属性名称)

外部代码:

namespace ExternalLibrary1.Area1
{
    public interface Interface0: Interface1, Interface2
    {

    }

    public interface Interface1
    {
        double Item { get; set; }
    }

    public interface Interface2
    {
        double Item { get; set; }
    }

    public class Class0 : Interface0
    {
        double Item;
    }
}
我的代码:

Interface0 myObject = new Class1();
myObject.Item = 2.0;
//above line gives me compile error "Ambiguity between 'ExternalLibrary1.Area1.Interface1.Item' and 'ExternalLibrary1.Area1.Interface2.Item'
正如在我的代码中所看到的,我在尝试分配给
属性时遇到了一个模糊错误


我无法更改此库。我知道我想将该值分配给
接口1
。我有没有办法明确地指定这一点来防止编译错误?

对于设计
接口0
接口1
接口2
类型层次结构的人来说,这似乎是一个奇怪的决定。可以执行的操作是强制转换到(或分配到的引用)要为其设置属性的接口类型:

Interface1 myObject = new Class1();
myObject.Item = 2.0;

除了Asad的答案之外,如果您需要在
interface 0
上使用其他属性和方法,您也可以在分配任务时放弃它

Interface0 myObject = new Class1();
(myObject as Interface1).Item = 2.0;

在我看来,这是一个糟糕的设计决策选择一个接口,任何接口,只是不选择接口0。@HansPassant我应该提到我必须使用
Interface0
来处理它的其他属性,但是我通过强制转换到
Interface1
来只分配这些属性,从而使它工作起来。实际的代码要复杂得多(实际上涉及到十几个不同的接口)-我理解为什么接口是这样创建的,即使它可以以不同的方式完成。无论如何,你的回答很有效。谢谢是的,这就是我最终要做的,因为
interface 0
是我需要使用的主要工具。