Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.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# “错误”;CS0104:';数据类型';是';System.ComponentModel.DataAnnotations.DataType';和';CarlosAg.ExcelXmlWriter.DataType“;_C#_Asp.net - Fatal编程技术网

C# “错误”;CS0104:';数据类型';是';System.ComponentModel.DataAnnotations.DataType';和';CarlosAg.ExcelXmlWriter.DataType“;

C# “错误”;CS0104:';数据类型';是';System.ComponentModel.DataAnnotations.DataType';和';CarlosAg.ExcelXmlWriter.DataType“;,c#,asp.net,C#,Asp.net,我得到这个错误: “CS0104:‘数据类型’是之间的不明确引用 “System.ComponentModel.DataAnnotations.DataType”和 “CarlosAg.ExcelXmlWriter.DataType” 运行ASP.NET 4.0应用程序时。有谁能帮我解决这个问题吗?您遇到了一个符号冲突-在源文件的作用域中有两个类,它们都称为数据类型-一个在System.ComponentModel.DataAnnotations中,另一个在命名空间CarlosAg.ExcelX

我得到这个错误:

“CS0104:‘数据类型’是之间的不明确引用 “System.ComponentModel.DataAnnotations.DataType”和 “CarlosAg.ExcelXmlWriter.DataType”


运行ASP.NET 4.0应用程序时。有谁能帮我解决这个问题吗?

您遇到了一个符号冲突-在源文件的作用域中有两个类,它们都称为
数据类型
-一个在
System.ComponentModel.DataAnnotations
中,另一个在命名空间
CarlosAg.ExcelXmlWriter.DataType'

您需要执行以下操作之一来解决此问题:

1。在每次使用时明确提供完整的命名空间前缀,即。
System.ComponentModel.DataAnnotations.DataType
CarlosAg.ExcelXmlWriter.DataType

var cdt = new CarlosAg.ExcelXmlWriter.DataType();
var sdt = new System.ComponentModel.DataAnnotations.DataType();

或2.或使用名称空间/类型的

using SystemDT = System.ComponentModel.DataAnnotations;
using Carlos = CarlosAg.ExcelXmlWriter;

然后用名称空间别名标识类型,例如

var dt = new Carlos.DataType();
或3。您还可以在类级别使用别名:

using SystemDataType = System.ComponentModel.DataAnnotations.DataType;
using CarlosDataType = CarlosAg.ExcelXmlWriter.DataType;
...
var myObj = new CarlosDataType();

或4。如果不需要从两个名称空间中删除符号,则使用子句从
中删除未使用的名称空间

我更喜欢选项2——它使读者更清楚地看到存在名称空间冲突,而不会过于冗长(就像选项1一样)

编辑

回复:“我试图给出完整的前缀,但仍然出现错误”CS0138:A using namespace指令只能应用于命名空间; “CarlosAg.ExcelXmlWriter.DataType”是一种类型,而不是命名空间”

(都与上面的第2点有关)。错误消息指的是这样一种情况,在.Net中是不允许的(但在Java导入中是允许的)


根据我的回答,我建议您为名称空间添加别名,然后使用别名前缀消除2
DataType
s

之间的歧义。我也遇到了同样的问题。我使用的解决方案是删除所有using并重新创建引用。效果非常好。

您需要消除歧义并完全限定其中一个名称解决类型。我忘记了,在我的例子中,我在定义控制器路由时遇到了这个问题。
// i.e. This won't work, can't import at a class level unless it is aliased
using System.ComponentModel.DataAnnotations.DataType;