简体   繁体   English

通过强制转换C#进行动态转换

[英]C# dynamic conversion through cast operator

I have some code that I have no control of. 我有一些我无法控制的代码。 This code accepts an object parameter and attempts to cast it to a type known at compile time like this: 此代码接受一个对象参数,并尝试将其转换为编译时已知的类型,如下所示:

KnownType item = (KnownType) parameter;

Is it possible in C# to design a custom class MyClass (not derived from KnownType) that can be passed as parameter to the above code and be converted to KnownType by the above code, provided that MyClass can convert itself to KnownType using its member method: 是否可以在C#中设计一个自定义类MyClass (不是从KnownType派生),它可以作为参数传递给上面的代码并通过上面的代码转换为KnownType ,前提是MyClass可以使用其成员方法将自身转换为KnownType

protected KnownType ConvertToKnownType()
{
    // conversion code goes here
}

I have tried to implement a custom conversion operator like this: 我试图实现这样的自定义转换运算符:

public static implicit operator KnownType(MyClass source)
{
    KnownType result;
    // conversion goes here
    return result;
}

but it didn't work (it was not used). 但它不起作用(它没有使用)。 Am I right to assume that the cast operator only works when the source type, target type and conversion operators are known at compile time? 我是否正确地假设转换运算符仅在编译时知道源类型,目标类型和转换运算符时才起作用?

Edit: I initially didn't provide more details about the code doing the conversion because I think it is irrelevant and because I am mainly interested in the way the cast operator is implemented, ie does it take look at the runtime type to find an appropriate converter or is the decision made at compile time? 编辑:我最初没有提供有关执行转换的代码的更多详细信息,因为我认为这是无关紧要的,因为我主要对实现强制转换操作符的方式感兴趣,即是否需要查看运行时类型以找到合适的转换器还是在编译时做出的决定?

To clear things out, KnownType is in fact DataRowView , while MyClass is a wrapper class for DataRowView that has to derive from some other type. 为了清除问题, KnownType实际上是DataRowView ,而MyClassDataRowView的包装类,必须从其他类型派生。 MyClass keeps a reference to a DataRowView . MyClass保留对DataRowView的引用。 Instead of binding ComboBox.DataSource to a DataView , I bind it to an IList<MyClass> but I still need the ComboBox to be able to access the DataRowView column values as if I was binding an IList<DataRowView> . 我没有将ComboBox.DataSource绑定到DataView ,而是将其绑定到IList<MyClass>但我仍然需要ComboBox能够访问DataRowView列值,就好像我绑定了IList<DataRowView> Unfortunately the cast operator works the way I was afraid it would: it only takes into account compile time type information to do conversions (it does however use runtime type information when casting between types in the same inheritance tree). 不幸的是,强制转换操作符的工作方式与我担心的方式相同:它只考虑编译时类型信息来进行转换(但是在同一继承树中的类型之间进行转换时会使用运行时类型信息)。

No, it is not possible. 不,这是不可能的。 The cast provided would only succeed if you're able to derive your class from that class. 如果您能够从该类派生您的类,则提供的强制转换只会成功。 Any type of conversion not based on inheritance would require the class performing the conversion to do something different than what it's doing. 任何不基于继承的转换都需要执行转换的类执行与其正在执行的操作不同的操作。

Am I right to assume that the cast operator only works when the source type, target type and conversion operators are known at compile time? 我是否正确地假设转换运算符仅在编译时知道源类型,目标类型和转换运算符时才起作用?

Yes. 是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM