我需要具有通用方法的接口。但是,该接口的每个实现都会知道其类型。
class Program
{
static void Main(string[] args)
{
var specificContext = new SpecificContext();
var res = new SrvThatCantBeGeneric().GetValueFromSpecificContext(specificContext);
Console.WriteLine(res);
}
}
public class SrvThatCantBeGeneric : ISrvThatCantBeGeneric
{
public int GetValueFromSpecificContext<SpecificContext>(SpecificContext specificContext)
{
return specificContext.MyProperty; // <-- this is where it breaks
}
}
public interface ISrvThatCantBeGeneric
{
int GetValueFromSpecificContext<T>( T specificContext);
}
public class SpecificContext
{
public int MyProperty { get; set; } = 42;
}
由于某种原因,当我以上述方式进行操作时,它不起作用并指出:
'SpecificContext'不包含'MyProperty'的定义,找不到可以接受的扩展方法'MyProperty'接受类型为'SpecificContext'的第一个参数(您是否缺少using指令或程序集引用?)
为什么会发生这种情况以及如何解决?
我可以通过移动通用类型的定义来修复它,如下所示:
public class SrvThatCantBeGeneric : ISrvThatCantBeGeneric<SpecificContext>
{
public int GetValueFromSpecificContext(SpecificContext specificContext)
{
return specificContext.MyProperty; // <-- this is where it breaks
}
}
public interface ISrvThatCantBeGeneric<T>
{
int GetValueFromSpecificContext( T specificContext);
}
但这会破坏代码的其他部分,是行不通的。
编辑:简化我的代码
通过写:
public int GetValueFromSpecificContext<SpecificContext>(SpecificContext specificContext)
您“覆盖”作用域,这SpecificContext
是一个通用类型参数,它会掩盖具有此名称的类,因此它与:
public int GetValueFromSpecificContext<T>(T specificContext)
也许您想写:
public interface ISrvThatCantBeGeneric
{
int GetValueFromSpecificContext<T>(T specificContext) where T : SpecificContext;
}
public class SrvThatCantBeGeneric : ISrvThatCantBeGeneric
{
public int GetValueFromSpecificContext<T>(T specificContext) where T : SpecificContext
{
return specificContext.MyProperty;
}
}
public class SpecificContext
{
public int MyProperty { get; set; } = 42;
}
public class SpecificContextChild : SpecificContext
{
public SpecificContextChild()
{
MyProperty = 10;
}
}
测试
var server = new SrvThatCantBeGeneric();
var context = new SpecificContextChild();
Console.WriteLine(server.GetValueFromSpecificContext(context));
输出量
10
您也可以将接口和类提升为泛型,而不是方法本身
如果相关:
public interface ISrvThatCantBeGeneric<T> where T : SpecificContext
{
int GetValueFromSpecificContext(T specificContext);
}
public class SrvThatCantBeGeneric<T> : ISrvThatCantBeGeneric<T> where T : SpecificContext
{
public int GetValueFromSpecificContext(T specificContext)
{
return specificContext.MyProperty;
}
}
public class SpecificContext
{
public int MyProperty { get; set; } = 42;
}
public class SpecificContextChild : SpecificContext
{
public SpecificContextChild()
{
MyProperty = 10;
}
}
var server = new SrvThatCantBeGeneric<SpecificContextChild>();
var context = new SpecificContextChild();
Console.WriteLine(server.GetValueFromSpecificContext(context));
另外,如果您喜欢或需要,可以使用接口代替基类
public interface IContext
{
int MyProperty { get; set; }
}
非通用类版本:
public interface ISrvThatCantBeGeneric
{
int GetValueFromSpecificContext<T>(T specificContext) where T : IContext;
}
public class SrvThatCantBeGeneric : ISrvThatCantBeGeneric
{
public int GetValueFromSpecificContext<T>(T specificContext) where T : IContext
{
return specificContext.MyProperty;
}
}
public class SpecificContext : IContext
{
public int MyProperty { get; set; } = 42;
}
通用类版本:
public interface ISrvThatCantBeGeneric<T> where T : IContext
{
int GetValueFromSpecificContext(T specificContext);
}
public class SrvThatCantBeGeneric<T> : ISrvThatCantBeGeneric<T> where T : IContext
{
public int GetValueFromSpecificContext(T specificContext)
{
return specificContext.MyProperty;
}
}
public class SpecificContext : IContext
{
public int MyProperty { get; set; } = 42;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句