c#asp mvc如何获取派生类对象值

c#asp mvc如何获取派生类对象值,c#,asp.net,asp.net-mvc,ienumerable,idictionary,C#,Asp.net,Asp.net Mvc,Ienumerable,Idictionary,有人能帮助我们获取派生类对象的值吗 当我尝试从派生类(货币)强制转换到父类(实体)时,我得到null。 DB中的实际值: 转换为实体后为空: 公共类实体 { public System.Guid Id{get;set;} 公共字符串名称{get;set;} } 公共类货币:实体 { 公共货币() { this.BankServices=new HashSet(); } public new System.Guid Id{get;set;} 公共新字符串名称{get;set;} 公共虚拟IColl

有人能帮助我们获取派生类对象的值吗

当我尝试从派生类(货币)强制转换到父类(实体)时,我得到null。 DB中的实际值: 转换为实体后为空:

公共类实体
{
public System.Guid Id{get;set;}
公共字符串名称{get;set;}
}
公共类货币:实体
{
公共货币()
{
this.BankServices=new HashSet();
}
public new System.Guid Id{get;set;}
公共新字符串名称{get;set;}
公共虚拟ICollection银行服务{get;set;}
}
公共虚拟IEnumerable GetItems()
{
//检查缓存
Dictionary EntityData=GetCachedData();
//如果它不在缓存中,请从存储库中读取它
if(EntityData==null)
{
//获取数据
Dictionary EntityDataToCache=新字典();
//从存储库获取数据
IEnumerable entityDataFromDb=LoadData();
foreach(entityDataFromDb中的var项)
{
var itemValue=item;//全部为零http://prntscr.com/ahmxxo
var itemValue2=(货币)项;//实际值http://prntscr.com/ahmy9h
EntityDataToCache[(Guid)item.GetType().GetProperty(“Id”).GetValue(item,null)]=项;
}
if(EntityDataToCache.Any())
{
//将此数据放入缓存30分钟
Set(CacheKey,EntityDataToCache,30);
}
}
var cache=cache.Get(CacheKey);
var result=缓存为字典;
返回结果值;
}
我建议阅读或做类似的事情,重新审视继承和隐藏之间的区别。在您的例子中,
Currency.Id
Currency.Name
属性上的
new
修饰符告诉编译器在父类中隐藏这些属性。因此,当您在
Currency
实例中分配这些属性时,这些值仅作为
Currency
的实例应用于该实例。从代码中可以看出,如果将该实例强制转换为
实体的实例
,对这些属性的引用就是对
实体
属性的引用(您尚未设置)。为了获得我认为您正在寻找的行为,您需要在
实体
中的属性声明中添加
虚拟
修饰符,然后将
新建
更改为
货币
中的
覆盖
。当然,如果您没有在
Currency
Entity
之间更改这些属性的行为,那么您根本不需要在
Currency
中重写它们……它们将可用于从
Entity
继承的所有类。希望对您有所帮助。

可能的副本
public class Entity
{
    public System.Guid Id { get; set; }
    public string Name { get; set; }
}

public class Currency:Entity
{
    public Currency()
    {
        this.BankServices = new HashSet<BankService>();
    }

    public new System.Guid Id { get; set; }
    public new string Name { get; set; }

    public virtual ICollection<BankService> BankServices { get; set; }
}

public virtual IEnumerable<Entity> GetItems()
{
    // check the cache
    Dictionary<Guid, Entity> EntityData = GetCachedData();

    // If it's not in the cache, read it from the repository
    if (EntityData == null)
    {
        // Get the data
        Dictionary<Guid, Entity> EntityDataToCache = new Dictionary<Guid, Entity>();

        // get data from the repository
        IEnumerable<Entity> entityDataFromDb = LoadData();
        foreach (var item in entityDataFromDb)
        {
            var itemValue = item; // ALL ZEROS http://prntscr.com/ahmxxo
            var itemValue2 = (Currency)item; // REAL VALUES http://prntscr.com/ahmy9h
            EntityDataToCache[(Guid)item.GetType().GetProperty("Id").GetValue(item, null)] = item;
        }

        if (EntityDataToCache.Any())
        {
            // Put this data into the cache for 30 minutes
            Cache.Set(CacheKey, EntityDataToCache, 30);
        }
    }

    var cache = Cache.Get(CacheKey);

    var result = cache as Dictionary<Guid, Entity>;
    return result.Values;
}