public IProduct Produce(RoomParts parts) { ProductListAttribute attr = (ProductListAttribute)Attribute.GetCustomAttribute(typeof(IProduct), typeof(ProductListAttribute)); foreach (var type in attr.ProductList) { ProductAttribute pa = (ProductAttribute)Attribute.GetCustomAttribute(type, typeof(ProductAttribute)); if (pa.RoomPart == parts) { object product = Assembly.GetExecutingAssembly().CreateInstance(type.FullName); return(product as IProduct); } } return(null); }
public IProduct Produce(RoomParts part) { // 通过反射从IProduct接口中获得属性从而获得所有产品列表 //参数1:Iproduct接口,type:ProductListAttribute ProductListAttribute attr = (ProductListAttribute)Attribute.GetCustomAttribute(typeof(IProduct), typeof(ProductListAttribute)); // 遍历所有的实现产品零件类型 foreach (var type in attr.ProductList) { // 利用反射查找其属性 ProductAttribute pa = (ProductAttribute)Attribute.GetCustomAttribute(type, typeof(ProductAttribute)); // 确定是否是需要到的零件 if (pa.RoomPart == part) { // 利用反射动态创建产品零件类型实例 object product = Assembly.GetExecutingAssembly().CreateInstance(type.FullName); return(product as IProduct); } } return(null); }