Reputation: 4353
I am trying to access the property of a subclass through reflection. But that is not working. How can I get access to all the properties of the subclass?
This is the class where I try to access the properties of the subclass through reflection. I tried class structure abstract and partial but both are not working.
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");
}
}
The subclass with the parameters
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();
}
}
Upvotes: 0
Views: 2512
Reputation: 73502
To get the private properties you need to use BindingFlags.NonPublic
using the overload of GetProperties which takes BindingFlags
as a parameter.
foreach (PropertyInfo property in GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic))
{
...
}
Upvotes: 3