我在以下代码上遇到编译错误(我调用_Context.OuterMethod(this)):
错误3参数1:无法从TestApp.Outer转换< T> .Inner< T>到TestApp.Outer< T> .Inner< T>
鉴于类之间的关系不应该起作用吗?
public class Outer<T>
where T : struct
{
public Outer()
{
_InnerObject = new Inner<T>(this);
}
private void OuterMethod(Inner<T> myInnerObject)
{
/* do something */
}
private Inner<T> _InnerObject; public Inner<T> InnerObject
{
get { return _InnerObject; }
}
public class Inner<T>
where T : struct
{
Outer<T> _Context;
public Inner(Outer<T> theContext)
{
_Context = theContext;
}
bool _SomeProperty;
public bool SomeProperty
{
get { return _SomeProperty; }
set
{
if(value != _SomeProperty)
{
_SomeProperty = value;
_Context.OuterMethod(this);
}
}
}
}
}
答案 0 :(得分:0)
鉴于评论,此代码现在按预期工作
public class Outer<T>
where T : struct
{
public Outer()
{
_InnerObject = new Inner(this);
}
private void OuterMethod(Inner myInnerObject)
{
/* do something */
}
private Inner _InnerObject; public Inner InnerObject
{
get { return _InnerObject; }
}
public class Inner
{
Outer<T> _Context;
public Inner(Outer<T> theContext)
{
_Context = theContext;
}
bool _SomeProperty;
public bool SomeProperty
{
get { return _SomeProperty; }
set
{
if (value != _SomeProperty)
{
_SomeProperty = value;
_Context.OuterMethod(this);
}
}
}
}
}