public void Emit( string propertyName, Type propertyType, Action <DynamicMethodBody> getmethod, Action <DynamicMethodBody> setmethod = null ) { PropertyBuilder property = dynamicTypeInfoField.TypeBuilder.DefineProperty( propertyName, PropertyAttributes.None, propertyType, new Type[] {} ); DynamicMethodInfo getMethodinfo = dynamicTypeInfoField .WithMethod(string.Format("get_{0}", propertyName)) .TurnOnAttributes(MethodAttributes.RTSpecialName) .TurnOnAttributes(MethodAttributes.SpecialName); getmethod(getMethodinfo.Returns(propertyType)); property.SetGetMethod(getMethodinfo.MethodBuilder); if (setmethod != null) { DynamicMethodInfo setMethodinfo = dynamicTypeInfoField .WithMethod(string.Format("set_{0}", propertyName)) .TurnOnAttributes(MethodAttributes.RTSpecialName) .TurnOnAttributes(MethodAttributes.SpecialName) .WithParameter(propertyType, "value"); setmethod(setMethodinfo.Returns(typeof(void))); property.SetSetMethod(setMethodinfo.MethodBuilder); } }
public void Returns_PassingAny_ShouldReturnSameValueOfBodyProperty() { // arrange var dmi = new DynamicMethodInfo(); var expected = dmi.Body; // act var result = dmi.Returns(typeof(int)); // assert result.Should().Be(expected); }
public static DynamicMethodBody NewMethod (Type returnType, params Type[] parameterTypes) { var result = new DynamicMethodInfo(); foreach (Type param in parameterTypes) { result.WithParameter(param); } result.Returns(returnType); return(result.Body); }