Итак, у меня есть класс, который является универсальным, и ему, возможно, потребуется внутри своего метода создать собственный экземпляр с другим типом универсального, тип которого получается посредством переопределения.
Это важно, потому что этот репозиторий отображает T в таблицу базы данных [это ORMish, который я пишу], и если класс, представляющий T, имеет коллекцию, представляющую ДРУГОЙ таблицу, мне нужно иметь возможность создать экземпляр этого и передать его в репозиторий [аля Начало].
Я предоставляю метод на тот случай, если вам будет легче увидеть проблему.
private PropertiesAttributesAndRelatedClasses GetPropertyAndAttributesCollection()
{
// Returns a List of PropertyAndAttributes
var type = typeof(T);
//For type T return an array of PropertyInfo
PropertiesAttributesAndRelatedClasses PAA = new PropertiesAttributesAndRelatedClasses();
//Get our container ready
PropertyAndAttributes _paa;
foreach (PropertyInfo Property in type.GetProperties())
//Let's loop through all the properties.
{
_paa = new PropertyAndAttributes();
//Create a new instance each time.
_paa.AddProperty(Property);
//Adds the property and generates an internal collection of attributes for it too
bool MapPropertyAndAttribute = true;
if (Property.PropertyType.Namespace == "System.Collections.Generic")
//This is a class we need to map to another table
{
PAA.AddRelatedClass(Property);
//var x = Activator.CreateInstance("GenericRepository", Property.GetType().ToString());
}
else
{
foreach (var attr in _paa.Attrs)
{
if (attr is IgnoreProperty)
//If we find this attribute it is an override and we ignore this property.
{
MapPropertyAndAttribute = false;
break;
}
}
}
if (MapPropertyAndAttribute)
PAA.AddPaa(_paa);
//Add this to the list.
}
return PAA;
}
Так дано GenericRepository, и я хочу создать GenericRepository, как мне это сделать? Строка, которую мне нужно заменить на что-то, что РАБОТАЕТ, это
// var x = Activator.CreateInstance("GenericRepository", Property.GetType().ToString());