생성 된 CIL로 처음 보니 제 무지와 함께 견뎌주십시오. 나는 POCO의 필드를 읽고 object[]
으로 채울 수있는 간단한 DynamicMethod를 찾고있다. 유형 변환은 필요하지 않습니다. 내가 할 수있는 모든 것을 모아 봤어, 그걸 완성하도록 도와 줄 수 있니?POCO를 CIL을 사용하여 배열로 변환하는 방법은 무엇입니까?
Type t = typeof(POCO);
DynamicMethod dm = new DynamicMethod("Get" + memberName,typeof(MemberType), new Type[] { objectType }, objectType);
ILGenerator il = dm.GetILGenerator();
// Load the instance of the object (argument 0) onto the stack
il.Emit(OpCodes.Ldarg_0);
// get fields
FieldInfo[] fields = t.GetFields();
// how do I create an array (object[]) at this point?
// per field
foreach (var pi in fields) {
// Load the value of the object's field (fi) onto the stack
il.Emit(OpCodes.Ldfld, fi);
// how do I add it into the array?
}
// how do I push the array onto the stack?
// return the array
il.Emit(OpCodes.Ret);
표현 트리를 사용하는 간단한 해결책이 있습니다. 그러면 컴파일 된 람다가됩니다. 관심을 보이시겠습니까? 아니면 특별히 방출 솔루션을 찾고 계십니까? – dasblinkenlight
IL에서 어떤 작업을 수행하는 방법을 알고 싶다면 C#에서 동일한 코드를 작성하고 생성 된 IL을 살펴보십시오. – svick
@Geotarget LINQ와 Linq 표현식을 혼동스럽게 생각합니다. 컴파일 된 표현식 트리는 수동으로 내보내는 코드와 동일하게 빠릅니다. – dasblinkenlight