나는 내가 FieldInfo
개체를 입수하고 그 위에 GetValue()
라고 한 어떤 끔찍한 DI 반사 :(내가 알고있는 객체가 System.RuntimeType 인 경우 입력 된 객체를 어떻게 가져올 수 있습니까?
을했습니다.
은 내가object
다시 얻을.
으로 VisualStudio에서이 개체를 검사, 나는 그것은 "프로토 타입"입니다 것을 발견하고, GetType()
반환 System.RuntimeType
. 직접 실행 창에
, 내 object as System.RuntimeType
캐스팅 할 수있는, 그리고 난 승, AsType()
방법을 발견 hich는 내 진짜, 입력 된 개체를 반환합니다.
불행하게도, 시도 내 코드에서 이것을 쓸 때, 나는 :(System.RuntimeType
내부 형, 따라서 나는 그것을 사용할 수 없다는 말거야. 내가 밖으로 입력 된 개체를 얻을 수있는 방법
을 이 객체의 전체 코드
: 내가 진정으로 실제로 개체의 유형이 무엇인지 모르는
KernelBase baseKernel = (KernelBase)ninjectKernel;
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
FieldInfo field = typeof(KernelBase).GetField("bindings", flags);
Multimap<Type, IBinding> allBindingsPerTypeMap = (Multimap<Type, IBinding>)field.GetValue(baseKernel);
var aBinding = allInterfaceTypesWithAllBindings.First(pair => pair.InterfaceType.Name.Contains("FilePath")).Binding;
var aBindingTarget = aBinding .BindingConfiguration.ProviderCallback.Target;
var prototype = aBindingTarget.GetType().GetField("prototype").GetValue(aBindingTarget);
// prototype is an object, which I'd like to turn into a real type.
var runtimeType = prototype.GetType(); // this returns System.RuntimeType
주 - 나는에 반영하고있어 것은, 그 자체의 조각 레 flection ... 내 DI 프레임 워크에서 바인딩 정보를 추출하려고합니다.
루트 문제에 대한 솔루션이 유용하지만 실제로이 System.RuntimeType
개체와 상호 작용하는 방법을 알고 싶습니다.
는 UPDATE : EVK는 "moah 반사!"제안 솔루션, 실제로 작동합니다. (댓글보기)
비 반사 방식이 있다면 나는 그것을 알고 싶어 할 것입니까?
다른 리플렉션을 통해 리플렉션을 사용 중입니다. 그런 다음 "내부적이기 때문에 사용할 수없는 유형"이라고 말합니다. 아무 것도 도움이되지 않으면 반사를 다시 사용할 수 있습니다. – Evk
D' oh! 명백하게! 'prototype.GetType(). GetMethod ("AsType"). Invoke (prototype, null)는 실제로 작동합니다. – Brondahl
@Evk 크레딧을 원하면 자유롭게 답변을 게시하십시오. – Brondahl