询问者
关于逆变转协变的问题

问题
-
这个是模型
public abstract class BaseModel { }
public class Model : BaseModel
{
}public interface IUCOut<out T> where T : BaseModel { IEnumerable<T> GetList(); void SetMyControl(IUCIn<T> my); } public interface IUCIn<in T> where T : BaseModel { void AddItems(IEnumerable<T> items); void RemoveItemAt(int index); void MoveItemSquence(int index, string direction); }
public partial class UC1 : IUCIn<Model>,IUCOut<Model> { #region IUCIn<Model> 成员 ………… #endregion #region IUCOut<Model> 成员 public IEnumerable<Model> GetList() { throw new NotImplementedException(); } public void SetMyControl(IUCIn<Model> my) { my = this;//转出来的为空 } #endregion
IUCOut<BaseModel> _out = UC1 as IUCOut<BaseModel>;//协变 IUCIn<BaseModel> _in; _out.SetMyControl(_in);//将UC1对像转为逆变是返回空
全部回复
-
您好,请使用ref。
public void SetMyControl(ref IUCIn<Model> my) { my = this;//转出来的为空 } UC1 myUc1 = new UC1(); IUCOut<BaseModel> _out = myUc1 as IUCOut<BaseModel>;//协变 IUCIn<Model> _in = null; myUc1.SetMyControl(ref _in); //将UC1对像转为逆变是返回空
以上說明若有錯誤請指教,謝謝。
亂馬客blog: http://www.dotblogs.com.tw/rainmaker/- 已建议为答案 ThankfulHeartModerator 2012年5月25日 6:38
- 已标记为答案 Alexander Sun 2012年5月29日 9:29
- 取消答案标记 evanlee 2012年6月1日 3:34
- 取消建议作为答案 ThankfulHeartModerator 2012年6月1日 4:41
-
不为空啊!你看我输出的!
namespace BinarySearch
{
public abstract class BaseModel
{
}
public class Model : BaseModel
{
}
public interface IUCOut<out T> where T : BaseModel
{
IEnumerable<T> GetList();
void SetMyControl(IUCIn<T> my);
}
public interface IUCIn<in T> where T : BaseModel
{
void AddItems(IEnumerable<T> items);
void RemoveItemAt(int index);
void MoveItemSquence(int index, string direction);
}
public partial class UC1 : IUCIn<Model>,IUCOut<Model>
{
public IEnumerable<Model> GetList()
{
return null;
}
public void SetMyControl(IUCIn<Model> my)
{
my = this;//
Console.WriteLine((my == null).ToString());
}
public void AddItems(IEnumerable<Model> items)
{
throw new NotImplementedException();
}
public void RemoveItemAt(int index)
{
throw new NotImplementedException();
}
public void MoveItemSquence(int index, string direction)
{
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
UC1 uc1 = new UC1();
IUCOut<BaseModel> _out = uc1 as IUCOut<BaseModel>;//协变
IUCIn<BaseModel> _in = null;
_out.SetMyControl(_in);
}
}
}
- 已编辑 ThankfulHeartModerator 2012年5月25日 6:13
-
楼主:)
这个和协变和反变似乎没有什么关系……你要记住——函数参数无论是传值还是传值(引用对象),函数参数都不是原来的那个版本,是赋值的。你可以把
(IUCIn<Model> my)
理解成一个副本,相当于内存中开辟了一个my的指针对象指向了this(自身),但是一旦出了函数体,my自动
被释放了。
注意:引用指针指向的对象是同一个对象(同一引用!),但是“指针”并不是同一个,一旦
改变了指针的指向对象,自然不是同一引用了,何况函数内的参数出了函数就无效咯
- 已编辑 ThankfulHeartModerator 2012年5月25日 6:40
-
在MSDN中,接口不是阐述为引用类型的么?
还是由于不是定义In的原因呢?
不是,这和In没有关系——你记住:
引用类型的定义是:对相同引用的对象,一方发生了改变,另一方也同步发生改变。
现在你看:
public void SetMyControl(IUCIn<Model> my) { my = this;//转出来的为空 }
假设主函数有一个接口(指针)引用a,传入SetMyControl中。那么内存中my=a(也就是my指向a)。
但是函数体内my=this(my的指针已经改变方向了)。此时a还是a自身,为空。
那么退出函数体之后,a还是a(my是函数传址,自然随着函数调用结束而结束)。
-