private void ImplementReadOnlyProperty( Expression backingField, Expression initialization, PropertyInfo interfaceProperty, string debuggerDisplayNameString, string debuggerDisplayString) { var name = MemberImplementationUtility.GetNameForExplicitImplementation(interfaceProperty); var getMethod = _concreteTarget.AddExplicitOverride(interfaceProperty.GetGetMethod(), ctx => Expression.Block(initialization, backingField)); var property = _concreteTarget.AddProperty(name, PropertyAttributes.None, getMethod, setMethod: null); _attributeGenerator.AddDebuggerDisplayAttribute(property, debuggerDisplayString, debuggerDisplayNameString); }
public void CreateProperty_Providers_ThrowsIfAlreadyExists() { var factory = new PropertyFactory(new MethodFactory(new RelatedMethodFinder())); Func <MethodBodyCreationContext, Expression> setBodyProvider = ctx => Expression.Empty(); var indexParameters = ParameterDeclarationObjectMother.CreateMultiple(2); var property = _mutableType.AddProperty("Property", typeof(int), indexParameters, setBodyProvider: setBodyProvider); Assert.That( () => factory.CreateProperty(_mutableType, "OtherName", property.PropertyType, indexParameters, 0, null, setBodyProvider), Throws.Nothing); Assert.That( () => factory.CreateProperty(_mutableType, property.Name, typeof(string), indexParameters, 0, null, setBodyProvider), Throws.Nothing); Assert.That( () => factory.CreateProperty( _mutableType, property.Name, property.PropertyType, ParameterDeclarationObjectMother.CreateMultiple(3), 0, null, setBodyProvider), Throws.Nothing); Assert.That( () => factory.CreateProperty(_mutableType, property.Name, property.PropertyType, indexParameters, 0, null, setBodyProvider), Throws.InvalidOperationException.With.Message.EqualTo("Property with equal name and signature already exists.")); }
public void DefineTypeFacets() { var typeInitializer = _mutableType.AddTypeInitializer(ctx => Expression.Empty()); var instanceInitialization = ExpressionTreeObjectMother.GetSomeExpression(); _mutableType.AddInitialization(ctx => instanceInitialization); var customAttribute = CustomAttributeDeclarationObjectMother.Create(); _mutableType.AddCustomAttribute(customAttribute); var @interface = typeof(IDisposable); _mutableType.AddInterface(@interface); var field = _mutableType.AddField(); var constructor = _mutableType.AddConstructor(); var method = _mutableType.AddMethod(); var property = _mutableType.AddProperty(); var event_ = _mutableType.AddEvent(); using (_mockRepository.Ordered()) { var context = PopulateContext(_generator, 2); _typeBuilderMock.Expect(mock => mock.SetParent(_mutableType.BaseType)); _memberEmitterMock.Expect(mock => mock.AddConstructor(context, typeInitializer)); _initializationBuilderMock.Expect(mock => mock.CreateInitializationMembers(_mutableType)).Return(_fakeInitializationMembers); _proxySerializationEnablerMock.Expect(mock => mock.MakeSerializable(_mutableType, _fakeInitializationMethod)); _typeBuilderMock.Expect(mock => mock.SetCustomAttribute(customAttribute)); _typeBuilderMock.Expect(mock => mock.AddInterfaceImplementation(@interface)); _memberEmitterMock.Expect(mock => mock.AddField(context, field)); _initializationBuilderMock.Expect( mock => mock.WireConstructorWithInitialization(constructor, _fakeInitializationMembers, _proxySerializationEnablerMock)); _memberEmitterMock.Expect(mock => mock.AddConstructor(context, constructor)); _memberEmitterMock.Expect(mock => mock.AddMethod(context, method)); SetupExpectationsForAccessors(_memberEmitterMock, _mutableType.AddedMethods.Except(new[] { method })); _memberEmitterMock.Expect(mock => mock.AddProperty(context, property)); _memberEmitterMock.Expect(mock => mock.AddEvent(context, event_)); } _mockRepository.ReplayAll(); _generator.DefineTypeFacets(); _mockRepository.VerifyAll(); }
public static MutablePropertyInfo AddProperty2( this MutableType mutableType, string name = null, PropertyAttributes attributes = PropertyAttributes.None, MutableMethodInfo getMethod = null, MutableMethodInfo setMethod = null) { name = name ?? "Property_" + ++s_counter; if (getMethod == null && setMethod == null) { getMethod = MutableMethodInfoObjectMother.Create(mutableType, "Getter", returnType: typeof(int)); } return(mutableType.AddProperty(name, attributes, getMethod, setMethod)); }
public static MutablePropertyInfo AddProperty( this MutableType mutableType, string name = null, Type type = null, IEnumerable <ParameterDeclaration> indexParameters = null, MethodAttributes accessorAttributes = MethodAttributes.Public, Func <MethodBodyCreationContext, Expression> getBodyProvider = null, Func <MethodBodyCreationContext, Expression> setBodyProvider = null) { name = name ?? "Property_" + ++s_counter; type = type ?? typeof(int); indexParameters = indexParameters ?? ParameterDeclaration.None; if (getBodyProvider == null && setBodyProvider == null) { getBodyProvider = ctx => Expression.Default(ctx.ReturnType); } return(mutableType.AddProperty(name, type, indexParameters, accessorAttributes, getBodyProvider, setBodyProvider)); }