C# 持续无知和懒惰加载
嘿,我找不到解决这个问题的办法: 我正在为EAV模式实现通用接口 要执行这样的代码:C# 持续无知和懒惰加载,c#,C#,嘿,我找不到解决这个问题的办法: 我正在为EAV模式实现通用接口 要执行这样的代码: Entity<GPS> gps = new Entity<GPS>("Path.To.GPS"); Console.WriteLine(gps.Attributes.Latitude); Console.WriteLine(gps.Attributes.Longitude); 实体gps=新实体(“Path.To.gps”); 控制台写入线(gps.Attrib
Entity<GPS> gps = new Entity<GPS>("Path.To.GPS");
Console.WriteLine(gps.Attributes.Latitude);
Console.WriteLine(gps.Attributes.Longitude);
实体gps=新实体(“Path.To.gps”);
控制台写入线(gps.Attributes.Latitude);
Console.WriteLine(gps.Attributes.Longitude);
其中:
public class GPS : IAttributes
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public abstract class Entity
{
public IAttributes Attributes { get; set; }
public string Path { get; set; }
public Entity(string path)
{
this.Path = path;
}
}
public class Entity<T> : Entity
where T : IAttributes, new()
{
private T attributes = new T();
new public T Attributes
{
get { return attributes; }
set { attributes = value; }
}
public Entity(string path)
: base(path)
{ }
}
public interface IAttributes
{
/* Empty by design */
}
公共类全球定位系统:国际航空运输协会
{
公共双纬度{get;set;}
公共双经度{get;set;}
}
公共抽象类实体
{
公共属性属性{get;set;}
公共字符串路径{get;set;}
公共实体(字符串路径)
{
this.Path=Path;
}
}
公共类实体:实体
其中T:IAttributes,new()
{
私有T属性=新的T();
新公共T属性
{
获取{返回属性;}
设置{attributes=value;}
}
公共实体(字符串路径)
:基本(路径)
{ }
}
公共接口和贡献
{
/*故意清空*/
}
它很容易添加到实体类方法中进行数据加载
迭代T中的所有参数并从某个源加载它们
简易路径:
Entity<GPS> gps = new Entity<GPS>("Path.To.GPS");
gps.Reload(); // <-- ADD THIS HERE
Console.WriteLine(gps.Attributes.Latitude);
Console.WriteLine(gps.Attributes.Longitude);
实体gps=新实体(“Path.To.gps”);
gps.Reload();// 你可以看一看。要使用它们,您需要定义要将代码附加到的属性作为虚拟属性。然后你就可以连接拦截器了。有关更多详细信息,请参阅