Tag: explicit

通用显式转换失败C#

我对以下代码有一些问题。 我想将一个字符串显式化为一个对象,这是完全正常的,但是,如果此对象是generics类的一部分,则失败并出现以下错误exception:“无法转换类型为’System.String’的对象’输入’test.B’“。 即使我已经重载了该方法。 using System; using System.Collections.Generic; namespace test { class Program { static void Main(string [] args) { // These two cast perfectly fine. B x = (B) “abc”; C y = (C) “def”; A a = new A(); ab(); A b = new A(); bb(); } } class A { public List a […]

显式铸造问题

// The Structure of the Container and the items public interface IContainer where TItem : IItem { } public class AContainer : IContainer { } public interface IItem { } public class ItemA : IItem { } // Client app [Test] public void Test () { IContainer container = new AContainer(); } 问题:在测试中发生以下错误。 什么可以成为铸造的解决方案? 无法隐式将“AContainer”类型转换为“IContainer”。 […]