반사를 사용하여 클래스 필드를 파악하고 채 웁니다. 현재 나는 Dictionary<,>
의 인스턴스를 감지하고 채우기 위해 Dictionary<object,object>
을 생성했습니다. 이후의 시도는 그러나이 작동하지 않는 유형을 변경하고 캐스팅 실패 :일반 사전을 알려진 유형으로 변환하는 방법은 무엇입니까?
// Looping through properties. Info is this isntance.
// Check is a dictionary field.
Dictionary<object, object> newDictionary = new Dictionary<object, object>();
// Populating the dictionary here from file.
Type[] args = info.PropertyType.GetGenericArguments();
info.GetSetMethod().Invoke(data, new object[]
{
newDictionary.ToDictionary(k => Convert.ChangeType(k.Key, args[0]),
k => Convert.ChangeType(k.Value, args[1]))
});
어떤 아이디어? 감사.
발견 한 사전의 일반적인 인스턴스를 만들어야합니다. 사용할 수없는 사전 인스턴스를 만들어야합니다. – user854301