0
我想通過反射訪問子類的屬性。但這不起作用。我怎樣才能訪問子類的所有屬性?C#:Reflection:子類的訪問屬性
這是我嘗試通過反射訪問子類屬性的類。 我試過類結構抽象和部分,但都沒有工作。
public abstract class FakeDbContext
{
public FakeDbSet<T> Set<T>() where T : class, IObjectState
{
foreach (PropertyInfo property in GetType().GetProperties())
{
if (property.PropertyType == typeof(FakeDbSet<T>))
return property.GetValue(this, null) as FakeDbSet<T>;
}
throw new Exception("Type collection not found");
}
}
子類與參數
public class MockDbContext : FakeDbContext
{
private FakeDbSet<Address> Addresses { get; set; }
private FakeDbSet<EmailAddress> EmailAddresses { get; set; }
private FakeDbSet<PhoneNumber> PhoneNumbers { get; set; }
private FakeDbSet<BaseContact> Contacts { get; set; }
private FakeDbSet<Environment> Environments { get; set; }
private FakeDbSet<Data.Entities.InformationService> InformationServices { get; set; }
private FakeDbSet<UserEnvironmentConfiguration> UserEnvironmentConfigurations { get; set; }
private FakeDbSet<Customer> Customers { get; set; }
private FakeDbSet<UserEnvironmentConfigurationSet> UserEnvironmentConfigurationSets { get; set; }
public MockDbContext()
{
Addresses = new FakeDbSet<Address>();
EmailAddresses = new FakeDbSet<EmailAddress>();
PhoneNumbers = new FakeDbSet<PhoneNumber>();
Contacts = new FakeDbSet<BaseContact>();
Environments = new FakeDbSet<Environment>();
InformationServices = new FakeDbSet<Data.Entities.InformationService>();
UserEnvironmentConfigurations = new FakeDbSet<UserEnvironmentConfiguration>();
Customers = new FakeDbSet<Customer>();
UserEnvironmentConfigurationSets = new FakeDbSet<UserEnvironmentConfigurationSet>();
InitData();
}
}