리플렉션을 사용하여 객체의 속성 값을 가져와야하는 시스템을 작성 중입니다. 이 프로젝트는 컴팩트 프레임 워크에서 실행되는 느린 가비지 컬렉터가있는 xbox360을위한 프로젝트이므로 할당을 피하는 것이 절대적으로 중요합니다.가비지 않고 리플렉션하여 값 가져 오기
내가이 일을 발견하는 유일한 방법은 다음과 같습니다
Foo Something; //an object I want to get data from
PropertyInfo p; //get this via reflection for the property I want
object value = p.GetGetmethod().Invoke(Something, null);
//Now I have to cast value into a type that it should be
내가 2 이유로이 싫어하는 :
- 주조는 도공을위한, 제네릭 프로그래머 것은 분명 생성
- 입니다 쓰레기 때마다 나는 원시적 인 가치를 얻고 상자에 넣는다.
상자 프리미티브가 아닌 속성에서 값을 가져 오는 일반적인 방법이 있습니까? 존스 응답에 대응
이편집 ::, 자신의 블로그에서 도난이 코드가 할당이 발생하지 않는 문제가 해결 :
String methodName = "IndexOf";
Type[] argType = new Type[] { typeof(char) };
String testWord = "TheQuickBrownFoxJumpedOverTheLazyDog";
MethodInfo method = typeof(string).GetMethod(methodName, argType);
Func<char, int> converted = (Func<char, int>)Delegate.CreateDelegate
(typeof(Func<char, int>), testWord, method);
int count = GC.CollectionCount(0);
for (int i = 0; i < 10000000; i++)
{
int l = converted('l');
if (GC.CollectionCount(0) != count)
Console.WriteLine("Collect");
}
하, 나는 당신의 "도공"견적을 훔칠지도 모른다.) –