我想在一个受接口约束的泛型方法中访问新创建对象的属性:
public interface MyInterface
{
int ID { get; set; }
string Name { get; set; }
}
由于编译器知道“T”属于 MyInterface 类型,因此应该可以访问该接口的属性:
public T doSomething<T>(String value) where T : MyInterface, new()
{
T entity = new T();
entity.Name = value;
return entity;
}
但它说:T没有“名称”的定义
如果我可以在这里使用接口作为约束:为什么不能访问它的属性?