- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这是一个非常不常见的问题,肯定有很多解决方法,但我想了解实际发生的情况以及为什么它不起作用。
所以我在测试解决方案中有 3 个程序集,第一个程序集的类型为 ClassA:
public class ClassA
{
public string Name { get; set; }
}
第二个程序集引用第一个程序集并具有 B 类:
public class ClassB
{
public string Name { get; set; }
public static explicit operator ClassA(ClassB objB)
{
return new ClassA
{
Name = objB.Name
};
}
}
它有一个显式运算符可以强制转换为 ClassA 类型。假设我们出于某种原因不能使用继承,只能使用转换作为将一种类型转换为另一种类型的便捷方式。
现在,最后一个程序集引用了第二个程序集(而不是第一个!)并且类型为 ClassC:
public class ClassC
{
public string Name { get; set; }
public static explicit operator ClassB(ClassC objC)
{
return new ClassB
{
Name = objC.Name
};
}
}
出于与 ClassB 相同的原因,它使用显式转换运算符。
现在是有趣的部分:如果我尝试在我的代码中从 ClassC 转换为 ClassB,如下所示:
ClassC objC = new ClassC();
ClassB objB = (ClassB)objC;
我收到以下错误:
Error 1 The type 'FirstAssembly.ClassA' is defined in an assembly that is not referenced. You must add a reference to assembly 'FirstAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
我可以很容易地创建 ClassB 的新实例,并使用 ClassC 实例的值对其进行初始化(就像我在显式转换运算符中所做的那样),它会工作得很好。那么这里有什么问题呢?
最佳答案
在 C# 语言规范(4.0 版)的 6.4.5 用户定义的显式转换部分中,它显示:
A user-defined explicit conversion from type S to type T is processed as follows:
• Determine the types S0 and T0. If S or T are nullable types, S0 and T0 are their underlying types, otherwise S0 and T0 are equal to S and T respectively.
• Find the set of types, D, from which user-defined conversion operators will be considered. This set consists of S0 (if S0 is a class or struct), the base classes of S0 (if S0 is a class), T0 (if T0 is a class or struct), and the base classes of T0 (if T0 is a class).
它没有定义如何编译器将“查找类型集”,但我认为它会搜索所有相关类以寻找下一步的候选对象:
• Find the set of applicable user-defined and lifted conversion operators, U. This set consists of the user-defined and lifted implicit or explicit conversion operators declared by the classes or structs in D that convert from a type encompassing or encompassed by S to a type encompassing or encompassed by T. If U is empty, the conversion is undefined and a compile-time error occurs.
这会导致它尝试解析对 ClassA 的引用。
关于c# - 显式转换运算符失败,出现 "assembly is not referenced"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22986300/
我不确定这个语法是否是 Informix 特有的,但是我在创建触发器时遇到了麻烦,直到我找到了一些包含这些行之一或两者的语法: CREATE TRIGGER accuplacer_trig
我是 Android Studio 和 gradle 的新手。我有一个 Android 项目,部分在 Eclipse 中工作。原始代码在Android中使用derby数据库。旧代码通过 JDBC 直接
我对以下示例中的行为感到困惑。我在一个项目中偶然发现了这个问题,我花了几个小时将问题缩小到一个简单的例子。所以这是我的简单测试类: 'John']]; public function inf
我在 pgAdmin 4 中创建了一些表,但由于某种原因我一直收到标题错误,你能找出原因吗?我没有看到任何问题,并且我已经查看了与我的代码示例相似的其他代码示例,这些示例编译得很好。它在 IDEone
我正在创建一个表,但出现此错误: number of referencing and referenced columns for foreign key disagree. 不知道怎么解决。我认为声
假设我有一个名为“C”的库 (.NETStandard 2.0),它定义了一个名为“CRecord”(记录)的类型。 假设我从名为“B”的 .NET 4.7.2 库中使用这个库。有一个使用“CReco
我已经尝试解决这个错误几分钟了,但我不知道表定义中缺少什么。 表格的代码如下: 表Autocare: CREATE TABLE [dbo].[Autocare] ( [IDAutocar]
DROP DATABASE IF EXISTS ProviderPatients; CREATE DATABASE ProviderPatients; USE ProviderPatients; CR
假设我有这个表stuff_property: | stuff_id (fk) | property_id | | ------------- | ----------- | 现在我想要进行查询,该查询
我是 MySQL 的新手。我完全有能力进行查询和创建表,但之前从未尝试过触发器。 CREATE TRIGGER TrigMora AFTER INSERT ON cliente REFEREN
我可以创建一个具有四个这样的属性的对象 $pocketKnife = New-Object PSObject -property @{ Color = 'Black' Weight =
我有一个名为 App 的对象,它包含主干应用程序的所有相关部分。 问题:当我从应用程序中的其他对象调用应用程序中的对象时,它们是未定义的。 我认为发生这种情况是因为在定义其自身的 App 对象之前,它
我有一个ArrayClass并且mergeSortArray扩展了它。并且 mergeSortArray 包含一个 mergeSort() 方法。但是,由于我使用 super 从父类(super cl
public class foo{ private String label; foo(String whereto){ label = whereto; } publi
我正在尝试以编程方式将库添加到引用的库中。这是我的代码: String filename = "myfile.jar"; InputStream is; try { is = new Buffe
我使用@Reference来获取我需要的所有信息: 吗菲亚: Query query = INSTANCE.createQuery(User.class); return query.asLi
我在 eclipse 中有一个 Java 项目,我想在其中添加 3 个 jar 文件到构建路径: 但是,当我选择它们并将它们添加到构建路径(右键单击/构建路径/添加到构建路径)时,它们将与成为类的“j
希望获得 Java 遵循的一些幕后内存引用和规则。 这是一段代码。基本上,此类用于实例化一些其他对象 (MyOtherObject),然后将此对象的 doClose() 方法的引用发送到 Vector
我打开了this关于转发引用的帖子,这是一个(希望如此)MCVE 代码: #include #include using namespace std; struct MultiMemoizator
MySQL 表: categoryID categoryName categoryParent 每个类别都有一个父类别,尽管它可以是 NULL,我将其视为根类别。 我想从表中获取所有类别,将其存储
我是一名优秀的程序员,十分优秀!