我有一个C#类,如下所示:
public class SpproRepository<T> : IRepository where T : ISpproEntity
{
// Code here
}
我有一个类似于
的上下文类public class SeymourSpContext : SpproBaseContext
{
public virtual SpproRepository<Properties> Properties { get; set; }
}
现在我正在尝试实例化所有属于SpproRepository类型的属性(我没有定义T,除了它实现了ISpproEntity。
我的问题很简单,我无法弄清楚如何检查属性是否为SpproRepository类型
这是我多次尝试之一:
public SpproBaseContext(string siteUrl)
{
var type = this.GetType();
foreach (var property in type.GetProperties()) //Itterate Through all properties, if they are type SpproRepository instantiate
{
if (property.PropertyType.GetInterfaces().Where(a=> a.GetType() == typeof(IRepository)).Any()) //I Implemented IRepository as a test, this didnt work. Ideally it would be something like Property.PropertyType is SpproRepository<>; but this doesnt either
{
//code below here works as it should
var listType = typeof(SpproRepository<>);
var genericArgs = property.PropertyType.GetGenericArguments();
var concreteType = listType.MakeGenericType(genericArgs);
var listName = genericArgs[0].Name;
var newRepo = Activator.CreateInstance(concreteType, listName);
property.SetValue(this, newRepo);
}
}
}