Kann jemand helfen, wie abgeleitete Klassenobjekte Werte erhalten?C# asp mvc wie abgeleitete Klassenobjektwerte erhalten
Wenn ich versuche, von der abgeleiteten Klasse (Currency) in die Elternklasse (Entity) zu konvertieren, erhalte ich Nullen. Reale Werte von DB: http://prntscr.com/ahmy9h Null sind nach dem Entity Gießen: http://prntscr.com/ahmxxo
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;
}
Mögliches Duplikat von ["new" Modifikator bewirkt, dass Basisimplementierungen NULL-Eigenschaftswerte haben] (http://stackoverflow.com/questions/6239790/new-modifier-causes-base-implementations-to-have-null-property -Werte) – cokeman19