Vì vậy, tôi có một lớp chung chung và nó có thể cần, bên trong một phương thức của nó, tự tạo ra một thể hiện của chính nó với một kiểu generic khác. . Điều này quan trọng bởi vì Repository này ánh xạ T tới một bảng cơ sở dữ liệu [đó là một ORMish tôi đang viết] và nếu lớp đại diện cho T có một bộ sưu tập biểu diễn bảng KHÁC tôi cần để có thể thể hiện và chuyển nó đến kho lưu trữ [ala Inception].
Tôi đang cung cấp phương pháp trong trường hợp nó giúp việc xem vấn đề trở nên dễ dàng hơn.C# Tạo một kiểu generic chưa biết ở thời gian chạy
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;
}
Vì vậy, hãy cho GenericRepository và tôi muốn tạo một GenericRepository làm cách nào để thực hiện việc này? Dòng Tôi cần phải thay thế bằng một cái gì đó mà làm việc là
// var x = Activator.CreateInstance("GenericRepository", Property.GetType().ToString());
Làm thế nào là tài sản (một trong những loại 'Hệ thống .Collections.Generic') được khai báo trong mã C# của bạn? Đối số kiểu của nó là ' 'giống như đối số' GenericRepository 'sở hữu thuộc tính? –
dasblinkenlight
Không, về cơ bản nó chỉ là một bộ sưu tập chung của một lớp khác như là một tài sản trên một lớp. nghĩa là lớp Giáo viên có danh sách lớp học. Kho chứa lớp giáo viên và cũng phải xử lý lớp lớp, nhưng vì nó thực sự nhận được T nó phải tìm ra nó phải xử lý như thế nào bằng cách sử dụng sự phản chiếu – Jordan
Vì vậy, thuộc tính trong lớp 'Teacher' được khai báo là' List Lớp học {/ * getter và/hoặc setter * /} '? Sẽ không 'Activator.CreateInstance (Property.GetType())' làm việc sau đó? –
dasblinkenlight