private void AddAbcAttribute(IMutableInfo customAttributeTarget, string value) { var ctor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new AbcAttribute("")); var attribute = new CustomAttributeDeclaration(ctor, new object[] { value }); customAttributeTarget.AddCustomAttribute(attribute); }
public void Initialization() { var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute((ValueType)null)); var property = NormalizingMemberInfoFromExpressionUtility.GetProperty((DomainAttribute obj) => obj.Property); var field = NormalizingMemberInfoFromExpressionUtility.GetField((DomainAttribute obj) => obj.Field); var declaration = new CustomAttributeDeclaration( constructor, new object[] { 7 }, new NamedArgumentDeclaration(property, 7), new NamedArgumentDeclaration(field, "value")); Assert.That(declaration.Type, Is.SameAs(typeof(DomainAttribute))); Assert.That(declaration.Constructor, Is.SameAs(constructor)); Assert.That(declaration.ConstructorArguments, Is.EqualTo(new[] { 7 })); var actualNamedArguments = declaration.NamedArguments.Select(na => new { na.MemberInfo, na.Value }); var expectedNamedArguments = new[] { new { MemberInfo = (MemberInfo)property, Value = (object)7 }, new { MemberInfo = (MemberInfo)field, Value = (object)"value" } }; Assert.That(actualNamedArguments, Is.EqualTo(expectedNamedArguments)); }
public void OverrideExisting_AddCustomAttribute() { var type = AssembleType <DomainType> ( proxyType => { var existingProperty = NormalizingMemberInfoFromExpressionUtility.GetProperty((DomainType obj) => obj.ExistingProperty); var getMethod = proxyType.GetOrAddOverride(existingProperty.GetGetMethod()); var setMethod = proxyType.GetOrAddOverride(existingProperty.GetSetMethod()); var property = proxyType.AddProperty(existingProperty.Name, PropertyAttributes.None, getMethod, setMethod); var attributeCtor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new AbcAttribute("")); var customAttributes = new CustomAttributeDeclaration(attributeCtor, new object[] { "derived" }); property.AddCustomAttribute(customAttributes); }); var newProperty = type.GetProperty("ExistingProperty"); var instance = (DomainType)Activator.CreateInstance(type); Assert.That(instance.ExistingProperty, Is.Null); Assert.That(newProperty.GetValue(instance, null), Is.Null); newProperty.SetValue(instance, "Test", null); Assert.That(instance.ExistingProperty, Is.EqualTo("Test")); Assert.That(newProperty.GetValue(instance, null), Is.EqualTo("Test")); var attributeArgs = Attribute.GetCustomAttributes(newProperty, inherit: true).Cast <AbcAttribute>().Select(a => a.Arg); Assert.That(attributeArgs, Is.EquivalentTo(new[] { "base", "derived" })); }
public override void Participate(object id, IProxyTypeAssemblyContext proxyTypeAssemblyContext) { var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new ModifiedAssembledTypeAttribute()); var attribute = new CustomAttributeDeclaration(constructor, new object[0]); proxyTypeAssemblyContext.ProxyType.AddCustomAttribute(attribute); }
public void WithCustomAttribute() { var type = AssembleType <OriginalType> ( proxyType => { var mutableFieldInfo = proxyType.AddField("_fieldWithCustomAttributes", FieldAttributes.Private, typeof(int)); var attributeCtor = typeof(AddedAttribute).GetConstructor(new[] { typeof(string) }); var namedProperty = typeof(AddedAttribute).GetProperty("NamedPropertyArg"); var namedField = typeof(AddedAttribute).GetField("NamedFieldArg"); var customAttributeDeclaration = new CustomAttributeDeclaration( attributeCtor, new object[] { "ctorArg" }, new NamedArgumentDeclaration(namedProperty, 7), new NamedArgumentDeclaration(namedField, new[] { MyEnum.Other, MyEnum.Default })); mutableFieldInfo.AddCustomAttribute(customAttributeDeclaration); }); var field = type.GetField("_fieldWithCustomAttributes", BindingFlags.Instance | BindingFlags.NonPublic); var customAttributes = field.GetCustomAttributes(false); Assert.That(customAttributes, Has.Length.EqualTo(1)); var customAttribute = (AddedAttribute)customAttributes.Single(); Assert.That(customAttribute.CtorArg, Is.EqualTo("ctorArg")); Assert.That(customAttribute.NamedPropertyArg, Is.EqualTo(7)); Assert.That(customAttribute.NamedFieldArg, Is.EqualTo(new[] { MyEnum.Other, MyEnum.Default })); }
public void SetCustomAttribute() { var attributeCtor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new AbcAttribute(null)); var property = NormalizingMemberInfoFromExpressionUtility.GetProperty((AbcAttribute obj) => obj.StringProperty); var field = NormalizingMemberInfoFromExpressionUtility.GetField((AbcAttribute obj) => obj.IntField); var wasCalled = false; Action <CustomAttributeBuilder> setCustomAttributeMethod = customAttributeBuilder => { wasCalled = true; Assert.That(customAttributeBuilder, Is.Not.Null); CheckCustomAttributeBuilder( customAttributeBuilder, attributeCtor, new object[] { typeof(int) }, new[] { property }, new object[] { "def" }, new[] { field }, new object[] { 8 }); }; var adapterBasePartialMock = MockRepository.GeneratePartialMock <BuilderAdapterBase> (setCustomAttributeMethod); var declaration = new CustomAttributeDeclaration( attributeCtor, new object[] { typeof(int) }, new NamedArgumentDeclaration(property, "def"), new NamedArgumentDeclaration(field, 8)); adapterBasePartialMock.SetCustomAttribute(declaration); Assert.That(wasCalled, Is.True); }
public override void SetUp() { base.SetUp(); _signedType = typeof(int); _unsignedType = CreateUnsignedType(TypeAttributes.Class, typeof(object)); _signedInterfaceType = typeof(IMarkerInterface); _unsignedInterfaceType = CreateUnsignedType(TypeAttributes.Interface | TypeAttributes.Abstract, baseType: null); _signedDelegateType = typeof(Action); _unsignedDelegateType = typeof(Action <>).MakeGenericType(_unsignedType); var attributeCtor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new AbcAttribute(null)); _signedAttribute = new CustomAttributeDeclaration(attributeCtor, new object[] { _signedType }); _unsignedAttribute = new CustomAttributeDeclaration(attributeCtor, new object[] { _unsignedType }); _signedField = NormalizingMemberInfoFromExpressionUtility.GetField(() => DomainType.Field); _unsignedField = _unsignedType.GetField("field"); _signedCtor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainType()); _unsignedCtor = _unsignedType.GetConstructors().Single(); _signedMethod = NormalizingMemberInfoFromExpressionUtility.GetMethod(() => DomainType.Method()); _unsignedMethod = _unsignedType.GetMethod("method"); _signedVarArgsMethod = typeof(DomainType).GetMethod("VarArgsMethod"); _unsignedVarArgsMethod = _unsignedType.GetMethod("varargs"); }
public void CreateInstance_NamedArgument() { var declaration = new CustomAttributeDeclaration(_defaultCtor, new object[0], new NamedArgumentDeclaration(_property, 4711)); var instance = (AbcAttribute)declaration.CreateInstance(); Assert.That(instance.Property, Is.EqualTo(4711)); }
public void CreateInstance_CtorArgument() { ICustomAttributeData declaration = new CustomAttributeDeclaration(_ctorWithArgs, new object[] { 7 }); var instance = (AbcAttribute)declaration.CreateInstance(); Assert.That(instance.CtorArg, Is.EqualTo(7)); }
public void CreateInstance_NoArguments() { ICustomAttributeData declaration = new CustomAttributeDeclaration(_defaultCtor, new object[0]); var instance = CustomAttributeDataExtensions.CreateInstance(declaration); Assert.That(instance, Is.TypeOf <AbcAttribute>()); }
public CustomAttributeTreeNode( CustomAttributeDeclaration customAttribute ) : base( customAttribute, TreeViewImage.CustomAttribute ) { StringBuilder name = new StringBuilder( 256 ); name.Append( CustomAttributeHelper.Render( customAttribute ) ); this.Text = name.ToString(); }
public void AddDebuggerBrowsableAttribute(IMutableMember member, DebuggerBrowsableState debuggerBrowsableState) { ArgumentUtility.CheckNotNull("member", member); var attribute = new CustomAttributeDeclaration(s_debuggerBrowsableAttributeConstructor, new object[] { debuggerBrowsableState }); member.AddCustomAttribute(attribute); }
public void Initialization_WithNullArgument() { var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new DomainAttribute((ValueType)null)); var declaration = new CustomAttributeDeclaration(constructor, new object[] { null }); Assert.That(declaration.ConstructorArguments[0], Is.Null); }
public CustomAttributeTreeNode(CustomAttributeDeclaration customAttribute) : base(customAttribute, TreeViewImage.CustomAttribute) { StringBuilder name = new StringBuilder(256); name.Append(CustomAttributeHelper.Render(customAttribute)); this.Text = name.ToString(); }
public void AddConcreteMixinTypeAttribute(IMutableMember member, ConcreteMixinTypeIdentifier concreteMixinTypeIdentifier) { ArgumentUtility.CheckNotNull("member", member); ArgumentUtility.CheckNotNull("concreteMixinTypeIdentifier", concreteMixinTypeIdentifier); var attributeData = ConcreteMixinTypeAttribute.Create(concreteMixinTypeIdentifier).ConcreteMixinTypeIdentifierData; var attribute = new CustomAttributeDeclaration(s_concreteMixinTypeAttributeConstructor, new object[] { attributeData }); member.AddCustomAttribute(attribute); }
public void CreateInstance_NullArguments() { var ctorArg = new object[] { "x", null }; var declaration = new CustomAttributeDeclaration( _ctorWithArgs, new object[] { ctorArg }, new NamedArgumentDeclaration(_property, null)); var instance = (AbcAttribute)declaration.CreateInstance(); Assert.That(instance.CtorArg, Is.EqualTo(ctorArg)); Assert.That(instance.Property, Is.Null); }
public void SetCustomAttribute(CustomAttributeDeclaration customAttributeDeclaration) { ArgumentUtility.CheckNotNull("customAttributeDeclaration", customAttributeDeclaration); var emittableConstructorArguments = customAttributeDeclaration.ConstructorArguments.Select(MakeAttributeArgumentEmittable).ToArray(); var emittableNamedArguments = customAttributeDeclaration.NamedArguments.Select(MakeEmittable).ToArray(); var emittableDeclaration = new CustomAttributeDeclaration( customAttributeDeclaration.Constructor, emittableConstructorArguments, emittableNamedArguments); _customAttributeTargetBuilder.SetCustomAttribute(emittableDeclaration); }
public void AddGeneratedMethodWrapperAttribute(IMutableMember member, MethodInfo methodToBeWrapped) { ArgumentUtility.CheckNotNull("member", member); ArgumentUtility.CheckNotNull("methodToBeWrapped", methodToBeWrapped); var attribute = new CustomAttributeDeclaration( s_generatedMethodWrapperAttributeConstructor, new object[] { methodToBeWrapped.DeclaringType, methodToBeWrapped.Name, methodToBeWrapped.ToString() }); member.AddCustomAttribute(attribute); }
public void AddOverrideInterfaceMappingAttribute(IMutableMember member, MethodInfo overriddenMethod) { ArgumentUtility.CheckNotNull("member", member); ArgumentUtility.CheckNotNull("overriddenMethod", overriddenMethod); var attribute = new CustomAttributeDeclaration( s_overrideInterfaceMappingAttributeConstructor, new object[] { overriddenMethod.DeclaringType, overriddenMethod.Name, overriddenMethod.ToString() }); member.AddCustomAttribute(attribute); }
public void AddCustomAttribute(CustomAttributeDeclaration customAttribute) { ArgumentUtility.CheckNotNull("customAttribute", customAttribute); if (_addedCustomAttributes.Any(a => a.Type == customAttribute.Type && !AttributeUtility.IsAttributeAllowMultiple(a.Type))) { var message = string.Format("Attribute of type '{0}' (with AllowMultiple = false) is already present.", customAttribute.Type.Name); throw new InvalidOperationException(message); } _addedCustomAttributes.Add(customAttribute); }
public void AddIntroducedMemberAttribute(IMutableMember member, MemberInfo interfaceMember, MemberDefinitionBase implementingMember) { ArgumentUtility.CheckNotNull("member", member); ArgumentUtility.CheckNotNull("interfaceMember", interfaceMember); ArgumentUtility.CheckNotNull("implementingMember", implementingMember); var attribute = new CustomAttributeDeclaration( s_introducedMemberAttributeConstructor, new object[] { implementingMember.DeclaringClass.Type, implementingMember.Name, interfaceMember.DeclaringType, interfaceMember.Name }); member.AddCustomAttribute(attribute); }
public void AddAttribute(IMutableMember member, ICustomAttributeData attributeData) { ArgumentUtility.CheckNotNull("member", member); ArgumentUtility.CheckNotNull("attributeData", attributeData); var attribute = new CustomAttributeDeclaration( attributeData.Constructor, attributeData.ConstructorArguments.ToArray(), attributeData.NamedArguments.Select(CreateNamedArgumentDeclaration).ToArray()); member.AddCustomAttribute(attribute); }
public void CreateInstance_ComplexArguments() { var ctorArg = new object[] { "x", 7, typeof(int), new[] { MyEnum.C, MyEnum.A } }; var namedArg = new object[] { "z", 8, new[] { 1, 2, 3 }, new[] { typeof(double), typeof(string) }, MyEnum.B }; var declaration = new CustomAttributeDeclaration( _ctorWithArgs, new object[] { ctorArg }, new NamedArgumentDeclaration(_property, namedArg)); var instance = (AbcAttribute)declaration.CreateInstance(); Assert.That(instance.CtorArg, Is.EqualTo(ctorArg)); Assert.That(instance.Property, Is.EqualTo(namedArg)); }
public void AddConcreteMixedTypeAttribute(IMutableMember member, ClassContext classContext, IEnumerable <Type> orderedMixinTypes) { ArgumentUtility.CheckNotNull("member", member); ArgumentUtility.CheckNotNull("classContext", classContext); ArgumentUtility.CheckNotNull("orderedMixinTypes", orderedMixinTypes); var attributeData = ConcreteMixedTypeAttribute.FromClassContext(classContext, orderedMixinTypes.ToArray()); var attribute = new CustomAttributeDeclaration( s_concreteMixedTypeAttributeConstructor, new object[] { attributeData.ClassContextData, attributeData.OrderedMixinTypes }); member.AddCustomAttribute(attribute); }
public IModuleBuilder CreateModuleBuilder(string assemblyName, string assemblyDirectoryOrNull, bool strongNamed, string keyFilePathOrNull) { ArgumentUtility.CheckNotNullOrEmpty("assemblyName", assemblyName); var moduleBuilder = _moduleBuilderFactory.CreateModuleBuilder(assemblyName, assemblyDirectoryOrNull, strongNamed, keyFilePathOrNull); var attribute = new CustomAttributeDeclaration(s_nonApplicationAssemblyAttributeConstructor, new object[0]); moduleBuilder.AssemblyBuilder.SetCustomAttribute(attribute); return(moduleBuilder); }
public void AddDebuggerDisplayAttribute(IMutableMember member, string debuggerDisplayString, string debuggerDisplayNameStringOrNull) { ArgumentUtility.CheckNotNull("member", member); ArgumentUtility.CheckNotNullOrEmpty("debuggerDisplayString", debuggerDisplayString); // Debugger display name may be null. var attribute = new CustomAttributeDeclaration( s_debuggerDisplayAttributeConstructor, new object[] { debuggerDisplayString }, new NamedArgumentDeclaration(s_debuggerDisplayAttributeNameProperty, debuggerDisplayNameStringOrNull)); member.AddCustomAttribute(attribute); }
private static Action <object, IProxyTypeAssemblyContext> CreateModifyingAction(Action <object, IProxyTypeAssemblyContext> participateAction) { return((id, ctx) => { participateAction(id, ctx); if (ctx.ProxyType.AddedCustomAttributes.Count == 0) { var constructor = NormalizingMemberInfoFromExpressionUtility.GetConstructor(() => new TypeAssembledByIntegrationTestAttribute()); var attribute = new CustomAttributeDeclaration(constructor, new object[0]); ctx.ProxyType.AddCustomAttribute(attribute); } }); }
public void RecordType_PropertiesAttributes() { var expectedAttributeValue = "bing://foo"; var customAttribute = new CustomAttributeDeclaration(typeof(FooAttribute), new List <object> { expectedAttributeValue }.AsReadOnly()); var props = new[] { new StructuralFieldDeclaration("Foo", typeof(int), new List <CustomAttributeDeclaration> { customAttribute }.AsReadOnly()) }; var type = RuntimeCompiler.CreateRecordType(props, valueEquality: true); var property = type.GetProperty("Foo"); var actualAttribute = property.GetCustomAttribute <FooAttribute>(inherit: false); Assert.IsNotNull(actualAttribute); Assert.AreEqual(expectedAttributeValue, actualAttribute.Uri); }
public override bool Execute() { var enumerator = _annotationRepositoryService.GetAnnotationsOfType(typeof(ExporterAttribute), false, false); while (enumerator.MoveNext()) { var serializedType = enumerator.Current !.Value.ConstructorArguments[0].Value; var exportedType = serializedType.Value.GetTypeDefinition(); if (exportedType.CustomAttributes.Any(a => a.Constructor.Parent.GetTypeDefinition().Name == typeof(InheritedExportAttribute).FullName && a.ConstructorArguments.Count == 0)) { continue; } var module = Project.Module; var exportAttributeType = (INamedType)module.FindType(typeof(ExportAttribute)); var exportAttributeCtor = module.FindMethod(exportAttributeType, ".ctor", c => c.Parameters.Count == 1 && c.Parameters[0].ParameterType.GetReflectionName() == "System.Type"); var types = module.Types .OfType <TypeDefDeclaration>() .Where(t => t.IsClass() && !t.IsAbstract() && !t.ExportsType(exportedType) && (t.ImplementsInterface(exportedType) || t.InheritsBase(exportedType))); foreach (var type in types) { var export = new CustomAttributeDeclaration(exportAttributeCtor); export.ConstructorArguments.Add(new MemberValuePair( MemberKind.Parameter, 0, "contractType", serializedType)); type.CustomAttributes.Add(export); } } return(true); }
public void SetCustomAttribute(CustomAttributeDeclaration customAttributeDeclaration) { ArgumentUtility.CheckNotNull("customAttributeDeclaration", customAttributeDeclaration); var propertyArguments = customAttributeDeclaration.NamedArguments.Where(na => na.MemberInfo.MemberType == MemberTypes.Property).ToArray(); var fieldArguments = customAttributeDeclaration.NamedArguments.Where(na => na.MemberInfo.MemberType == MemberTypes.Field).ToArray(); var customAttributeBuilder = new CustomAttributeBuilder( customAttributeDeclaration.Constructor, customAttributeDeclaration.ConstructorArguments.ToArray(), propertyArguments.Select(namedArg => (PropertyInfo)namedArg.MemberInfo).ToArray(), propertyArguments.Select(namedArg => namedArg.Value).ToArray(), fieldArguments.Select(namedArg => (FieldInfo)namedArg.MemberInfo).ToArray(), fieldArguments.Select(namedArg => namedArg.Value).ToArray()); _setCustomAttributeMethod(customAttributeBuilder); }
private GeneratedTypesContext GenerateTypes(AssembledTypeID typeID, ProxyTypeAssemblyContext context, IMutableTypeBatchCodeGenerator codeGenerator) { // Add [AssembledType] attribute. var attribute = new CustomAttributeDeclaration(s_assembledTypeAttributeCtor, new object[0]); context.ProxyType.AddCustomAttribute(attribute); // Add '__typeID' and initialization code. _assembledTypeIdentifierProvider.AddTypeID(context.ProxyType, typeID); // Enable complex serialization. _complexSerializationEnabler.MakeSerializable(context.ProxyType, _participantConfigurationID, _assembledTypeIdentifierProvider, typeID); var mutableTypes = context.AdditionalTypes.Values.Concat(new[] { context.ProxyType }); return(GenerateTypesWithDiagnostics(codeGenerator, mutableTypes, context.RequestedType.Name)); }
private static void ImplementSetter( PropertyDeclaration property, TypeDefDeclaration type, IField field, MethodAttributes methodAttributes, CustomAttributeDeclaration compilerGenerated) { // Implement setter var setter = new MethodDefDeclaration { Attributes = methodAttributes, Name = "set_" + property.Name, CallingConvention = CallingConvention.HasThis, }; type.Methods.Add(setter); MarkCompilerGenerated(setter, compilerGenerated); setter.ReturnParameter = new ParameterDeclaration { ParameterType = type.Module.Cache.GetIntrinsic(IntrinsicType.Void), Attributes = ParameterAttributes.Retval }; setter.Parameters.Add(new ParameterDeclaration(0, "value", property.PropertyType)); var methodBody = new MethodBodyDeclaration(); var sequence = methodBody.CreateInstructionSequence(); var instructionBlock = methodBody.CreateInstructionBlock(); instructionBlock.AddInstructionSequence(sequence, NodePosition.After, null); methodBody.RootInstructionBlock = instructionBlock; setter.MethodBody = methodBody; var writer = new InstructionWriter(); writer.AttachInstructionSequence(sequence); writer.EmitInstruction(OpCodeNumber.Ldarg_0); writer.EmitInstruction(OpCodeNumber.Ldarg_1); writer.EmitInstructionField(OpCodeNumber.Stfld, field); writer.EmitInstruction(OpCodeNumber.Ret); writer.DetachInstructionSequence(); property.Members.Add(new MethodSemanticDeclaration(MethodSemantics.Setter, setter)); }
private static IField CreateBackingField( TypeDefDeclaration type, string name, ITypeSignature fieldType, CustomAttributeDeclaration compilerGenerated) { var field = new FieldDefDeclaration { Attributes = FieldAttributes.Private, Name = string.Format("<{0}>k__BackingField", name), FieldType = fieldType }; // Create a backing field type.Fields.Add(field); MarkCompilerGenerated(field, compilerGenerated); return GenericHelper.GetCanonicalGenericInstance(field); }
public static void MarkCompilerGenerated(this IMetadataDeclaration supportsCustomAttributes, CustomAttributeDeclaration compilerGenerated) { if (compilerGenerated == null) return; supportsCustomAttributes.CustomAttributes.Add(compilerGenerated); }
public static PropertyDeclaration ImplementPublicProperty( this TypeDefDeclaration type, string name, ITypeSignature propertyType, MethodAttributes extraAttributes, IEnumerable<CustomAttributeDeclaration> propertyAttributes, CustomAttributeDeclaration compilerGenerated) { var field = CreateBackingField(type, name, propertyType, compilerGenerated); var property = CreateProperty(type, name, propertyType, propertyAttributes); var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName; methodAttributes |= extraAttributes; ImplementGetter(property, type, field, methodAttributes, compilerGenerated); ImplementSetter(property, type, field, methodAttributes, compilerGenerated); return property; }
public static PropertyDeclaration ImplementPublicProperty( this TypeDefDeclaration type, string name, ITypeSignature propertyType, CustomAttributeDeclaration compilerGenerated) { return ImplementPublicProperty(type, name, propertyType, 0, null, compilerGenerated); }
public ParameterValidationAdvice(CustomAttributeDeclaration declaration, ParameterDeclaration parameterDeclaration, int parameterIndex) { this.declaration = declaration; this.parameterDeclaration = parameterDeclaration; this.parameterIndex = parameterIndex; }
public MethodValidationAdvice(CustomAttributeDeclaration attributeDeclaration) { this.attributeDeclaration = attributeDeclaration; }