I는 다음과 같습니다 서명이있는 방법이 있습니다어떻게 제네릭 형식 문자열을 변환하는
public IList<T> GetReferenceData<T>(TransactionManager transactionManager = null)
{
IList<T> collection;
var cacheData = DataCacheManager.Instance.GetCacheItem(typeof(T).Name);
if (cacheData != null)
{
collection = (IList<T>)cacheData;
}
else
{
collection = this.GetReferenceDataNoCache<T>(transactionManager);
DataCacheManager.Instance.AddCacheItem(typeof(T).Name, collection);
}
return collection;
}
나는 나를에 해당 문자열을 변환하는 문자열에 전달할 수 있습니다 다른 방법이 적절한 유형. 그런 다음 위의 메서드를 호출하고 싶습니다.
public IList GetReferenceDataByType(string referenceType)
{
// this works and returns the appropriate type correctly
var type = this.GetEntity(referenceType);
// now I'm stuck
return this.GetReferenceData<?>();
}
무엇이 물음표를 대체합니까?
public IList GetReferenceDataByType(string referenceType)
{
// this works and returns the appropriate type correctly
var type = this.GetEntity(referenceType);
var method = this.GetType().GetMethod("GetReferenceData");
var generic = method.MakeGenericMethod(type);
return (IList) generic.Invoke(this, new object[] { null });
}
주 IList<T>
즉, 그래서 캐스트가 실패 할 수 있습니다 IList
를 구현하지 않습니다
을 도움이되기를 바랍니다. –
@TimSchmelter 이것은 문제가되지 않습니다. 그는 이미 그 유형을 가지고 있습니다. 그는 변수 유형 매개 변수를 기반으로 완전히 해결 된 제네릭 메소드를 얻고 싶습니다. –
'GetReferenceData '본문을 게시 할 수 있습니까? –
Dennis