C# “C”;初始值设定项从“派生”到“基”;这是显而易见的团结,但不是其他
我不确定这是否是一个unity bug,但是有人能帮我解释为什么下面的程序在第三种情况下打印150C# “C”;初始值设定项从“派生”到“基”;这是显而易见的团结,但不是其他,c#,.net,dependency-injection,unity-container,derived-class,C#,.net,Dependency Injection,Unity Container,Derived Class,我不确定这是否是一个unity bug,但是有人能帮我解释为什么下面的程序在第三种情况下打印150 void Main() { // Test 1 Derived d = new Derived(); d.Height.Dump(); //Prints 10 // Test 2 IUnityContainer unityContainer = new UnityContainer(); unityContainer.Resolve(typeof(Derived)).Dump(); // Pri
void Main()
{
// Test 1
Derived d = new Derived();
d.Height.Dump(); //Prints 10
// Test 2
IUnityContainer unityContainer = new UnityContainer();
unityContainer.Resolve(typeof(Derived)).Dump(); // Prints 10
// Test 3
unityContainer.RegisterType<IPrintHeightService<Derived>, PrintHeightService<Derived>>();
var output = unityContainer.Resolve<IPrintHeightService<Derived>>();
output.Show(); //Prints 150
}
public interface IHasHeight
{
Int32 Height {get; set;}
}
// Define other methods and classes here
public class Default : IHasHeight
{
public Int32 Height {get; set;}
public Default()
{
Height = 150;
}
}
public class Derived : Default
{
public new Int32 Height {get { return 10;}}
}
public interface IPrintHeightService<T> where T:IHasHeight, new()
{
void Show();
}
public class PrintHeightService<T> : IPrintHeightService<T> where T:IHasHeight, new()
{
private IHasHeight objectWithHeight;
public PrintHeightService(IUnityContainer unityContainer)
{
objectWithHeight = (T) unityContainer.Resolve(typeof(T));
}
public void Show()
{
Console.WriteLine(objectWithHeight.Height); // Prints 150
}
}
void Main()
{
//测试1
派生d=新派生();
d、 Height.Dump();//打印10
//测试2
IUnityContainer unityContainer=新的unityContainer();
unityContainer.Resolve(typeof(派生)).Dump();//打印10
//测试3
unityContainer.RegisterType();
var output=unityContainer.Resolve();
output.Show();//打印150
}
公共接口ihashight
{
Int32高度{get;set;}
}
//在此处定义其他方法和类
公共类默认值:IHasHeight
{
公共Int32高度{get;set;}
公共违约()
{
高度=150;
}
}
派生的公共类:默认值
{
公共新Int32高度{get{return 10;}}
}
公共接口IPrintHeightService,其中T:IHasHeight,new()
{
void Show();
}
公共类PrintHeightService:IPrintHeightService,其中T:IHasHeight,new()
{
私人ihashweight物体的高度;
公共打印高度服务(IUnityContainer unityContainer)
{
objectWithHeight=(T)unityContainer.Resolve(typeof(T));
}
公开展览(
{
Console.WriteLine(objectWithHeight.Height);//打印150
}
}
我怀疑这是因为初始化器是按所解释的从派生到基运行的(http://blogs.msdn.com/b/ericlippert/archive/2008/02/18/why-do-initializers-run-in-the-opposite-order-as-constructors-part-two.aspx)但为什么这是显而易见的团结,而不是其他
非常感谢。这是因为您将高度声明为新高度。通过将其声明为
new
,派生不会覆盖高度,它只是将其隐藏。这与初始值设定者无关
这意味着新的实现与接口声明的虚拟属性没有任何关系,它只是碰巧有相同的名称,因此“隐藏”了它。通过调用Height overIHasHeight
(在PrintHeightService
的Show()
方法中访问该类型的字段),调用接口声明的属性,该属性在Default
中实现。如果通过类型为Derived
的引用调用它,则只能获得Derived.Height
您可以自己查看:将示例1中变量d
的类型更改为Default
或IHasHeight
,您也会看到得到150
如果您确实希望覆盖派生的中的高度的实现
- 在
Default
中将Height
声明为virtual
,然后
- 将
高度
声明为覆盖
中的派生
- 并删除新的
修改器
这是因为您将高度声明为新高度。通过将其声明为new
,派生不会覆盖高度,它只是将其隐藏。这与初始值设定者无关
这意味着新的实现与接口声明的虚拟属性没有任何关系,它只是碰巧有相同的名称,因此“隐藏”了它。通过调用Height overIHasHeight
(在PrintHeightService
的Show()
方法中访问该类型的字段),调用接口声明的属性,该属性在Default
中实现。如果通过类型为Derived
的引用调用它,则只能获得Derived.Height
您可以自己查看:将示例1中变量d
的类型更改为Default
或IHasHeight
,您也会看到得到150
如果您确实希望覆盖派生的中的高度的实现
- 在
Default
中将Height
声明为virtual
,然后
- 将
高度
声明为覆盖
中的派生
- 并删除新的
修改器
我删除了“new”关键字,但它没有帮助,如果您在PrintHeightService构造函数中打印typeof(t),您会看到它实际上是派生的type@RohitSharma通过仅删除new
关键字,您仍然隐藏了原始属性。你也应该得到一个警告。请参阅我编辑的答案,以了解真正重写该属性的方法。我认为你说的有道理,但让我困惑的是,为什么当我执行unityContainer.Resolve(typeof(派生))时,它会创建一个高度为10的对象,而当我执行objectWithHeight=(T)unityContainer.Resolve(typeof(T));其中T是“派生”类型,它以150作为高度创建?@RohitSharma它与unity容器没有任何关系。如果使用新修改器隐藏方法或属性,则只有调用它的变量的类型决定使用哪个属性。在printservice中,变量的类型为IHasHeight。在当前代码中,只有默认值实际实现IHasHeight.Height。Resolve(typeof(Derived))返回派生的,所以派生的。调用Height。如果调用objectWithHeight.Height,编译器不知道该对象是派生类型,因此它会查找默认实现的iHashHeight.Height。多谢大家,我想我又回到了基础:)我删除了“new”关键字,如果您打印typeof(t),则没有帮助在PrintHeightService构造函数中,您会看到它实际上是派生的type@RohitSharma通过仅删除new
关键字,您仍然隐藏了原始属性。你也应该得到一个警告。请参阅我编辑的答案,以了解真正重写该属性的方法。我认为你说的有道理,但让我困惑的是,为什么当我使用unityContainer.Resolve(typeof(派生))时,它会创建高度为10的对象,而当我使用高度=(T)unityContainer的对象时。