제네릭 형식 정의 사용
class MyClass
{
private Type _type = typeof(string);
public MyClass(Type type)
{
this._type = typeof(type);
this.MyList = Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
}
public IList MyList { get; set; } <----it likes this
}
: 당신이이 Type
인수에 전달 제네릭 당신이 뭔가를 할 수 있습니다 작동하지 않습니다 동의해야하는 경우
class MyClass<T>
{
private Type _type = typeof(string);
public MyClass()
{
this._type = typeof(T);
}
public List<T> MyList { get; set; } <----it likes this
}
을 장점은 목록에 유형 제약 조건을 적용한다는 것입니다. 지정된 유형의 항목 만 목록에 추가 할 수 있습니다. 단점은 당신이 얻는 모든 아이템을 던져야한다는 것입니다. 이런 일을 피하고 위의 일반적인 예를 사용하면 더 좋을 것입니다. 세 번째 옵션은 제네릭을 완전히 삭제하는 것입니다.
class MyClass
{
private Type _type = typeof(string);
public MyClass(Type type)
{
this._type = typeof(type);
this.MyList = new ArrayList();
}
public IList MyList { get; set; } <----it likes this
}
이것은 유형 적용을 제공하지 않습니다. 귀하의 마일리지가 다를 수 있습니다. 제네릭
출처
2013-06-03 19:03:29
Amy
가장 쉬운 해결책은 클래스가 일반적인 있도록하는 것입니다 ... –