私はFieldInfo.SetValueDirect
用リフレクターにチェックした場合は次のようになります。メソッドがNotImplementedExceptionのみを含み、投げずに実行する方法を教えてください。
のC#、.NET 4.0:
[CLSCompliant(false)]
public virtual void SetValueDirect(TypedReference obj, object value)
{
throw new NotSupportedException(Environment.GetResourceString("NotSupported_AbstractNonCLS"));
}
そしてILとして:
.method public hidebysig newslot virtual instance void SetValueDirect(valuetype System.TypedReference obj, object 'value') cil managed
{
.custom instance void System.CLSCompliantAttribute::.ctor(bool) = { bool(false) }
.maxstack 8
L_0000: ldstr "NotSupported_AbstractNonCLS"
L_0005: call string System.Environment::GetResourceString(string)
L_000a: newobj instance void System.NotSupportedException::.ctor(string)
L_000f: throw
}
しかし、私は次のコードを実行した場合、はちょうどの作品です。
// test struct:
public struct TestFields
{
public int MaxValue;
public Guid SomeGuid; // req for MakeTypeRef, which doesn't like primitives
}
[Test]
public void SettingFieldThroughSetValueDirect()
{
TestFields testValue = new TestFields { MaxValue = 1234 };
FieldInfo info = testValue.GetType().GetField("MaxValue");
Assert.IsNotNull(info);
// TestFields.SomeGuid exists as a field
TypedReference reference = TypedReference.MakeTypedReference(
testValue,
new [] { fields.GetType().GetField("SomeGuid") });
int value = (int)info.GetValueDirect(reference,);
info.SetValueDirect(reference, 4096);
// assert that this actually worked
Assert.AreEqual(4096, fields.MaxValue);
}
エラーは発生しません。 GetValueDirect
についても同様です。リソースの名前に基づいた私の推測は、コードがCLSCompliantでなければならない場合にのみスローされますが、メソッドの本体はどこですか?別の言い方をすれば、メソッドの実際の本体をどのように反映させることができますか?
Jonは軽度よりも速かったですが、結論は彼と同じです。 – Abel