private void InitializeWithInjectedFixture(
     [Frozen] IArgumentNullExceptionFixture fixture,
     CustomAttribute sut)
 {
     // Assert
     Assert.Same(fixture, sut.Fixture);
 }
        private void ReturnsEnumerationOfMethodData(
            [Frozen] Mock<IArgumentNullExceptionFixture> fixtureMock,
            ICollection<MethodData> expectedData,
            MethodInfo unused1,
            CustomAttribute sut)
        {
            // Arrange
            fixtureMock.Setup(f => f.GetData()).Returns(expectedData);

            // Act
            List<MethodData> actualData =
                sut.GetData(unused1)
                   .SelectMany(d => d.OfType<MethodData>())
                   .ToList();

            // Assert
            Assert.Equal(expectedData.Count, actualData.Count);
            Assert.False(expectedData.Except(actualData).Any());
        }
Exemplo n.º 3
0
 bool IsDefined(Type attributeType, bool inherit)
 {
     return(CustomAttribute.IsDefined(this, attributeType, inherit));
 }
Exemplo n.º 4
0
 public override object[] GetCustomAttributes(bool inherit)
 {
     return(CustomAttribute.GetCustomAttributes(this, typeof(object) as RuntimeType));
 }
Exemplo n.º 5
0
 private static void VerifyPseudoCustomAttribute(Type pca)
 {
     CustomAttribute.GetAttributeUsage(pca as RuntimeType);
 }
Exemplo n.º 6
0
 static private extern CustomAttribute GetCustomAttributeList(int token, IntPtr module, RuntimeType caType, CustomAttribute caItem, int level);
Exemplo n.º 7
0
 internal static IList <CustomAttributeData> GetCustomAttributesInternal(RuntimeParameterInfo target)
 {
     return(CustomAttribute.GetCustomAttributesData(target));
 }
Exemplo n.º 8
0
        private static void ProcessMethod(ModuleDefinition module, MethodDefinition method, CustomAttribute aspect)
        {
            method.Body.SimplifyMacros();

            MethodReference aspectConstructorRef = aspect.Constructor;

            MethodInfo getCurrentMethod = typeof(MethodBase).GetMethod("GetCurrentMethod", BindingFlags.Public | BindingFlags.Static);
            MethodReference getCurrentMethodRef = module.Import(getCurrentMethod);

            TypeReference methodInfoRef = module.Import(typeof(MethodInfo));
            TypeReference objectRef = module.Import(typeof(Object));

            MethodReference methodExecutionArgsCtorRef =
                module.Import(typeof(MethodExecutionArgs).GetConstructor(new Type[]
                    {
                        typeof(MethodInfo), typeof(object[])
                    }));

            MethodReference onEntryRef =
                module.Import(typeof(OnMethodBoundaryAspect).GetMethod("OnEntry"));

            MethodReference onSuccessRef =
                module.Import(typeof(OnMethodBoundaryAspect).GetMethod("OnSuccess"));

            MethodReference onExceptionRef =
                module.Import(typeof(OnMethodBoundaryAspect).GetMethod("OnException"));

            MethodReference onExitRef =
                module.Import(typeof(OnMethodBoundaryAspect).GetMethod("OnExit"));

            MethodInfo getFlowBehavior =
                typeof(MethodExecutionArgs).GetProperty("FlowBehavior").GetGetMethod();
            MethodReference getFlowBehaviorRef = module.Import(getFlowBehavior);

            MethodInfo setException =
                typeof(MethodExecutionArgs).GetProperty("Exception").GetSetMethod();
            MethodReference setExceptionRef = module.Import(setException);

            MethodInfo getException =
                typeof(MethodExecutionArgs).GetProperty("Exception").GetGetMethod();
            MethodReference getExceptionRef = module.Import(getException);

            MethodInfo getParameters = typeof(MethodBase).GetMethod("GetParameters");
            MethodReference getParametersRef = module.Import(getParameters);

            var processor = method.Body.GetILProcessor();
            var firstInstruction = method.Body.Instructions[0];

            // Original last instruction, change to a Nop.
            method.Body.Instructions.Last().OpCode = OpCodes.Nop;

            var finalInstruction = processor.Create(OpCodes.Nop);
            method.Body.Instructions.Add(finalInstruction);
            method.Body.Instructions.Add(processor.Create(OpCodes.Ret));

            // Add necessary variables
            int existingVariableCount = method.Body.Variables.Count;

            method.Body.Variables.Add(new VariableDefinition("__aspect", aspect.AttributeType)); // module.Import(aspect.GetType())));
            method.Body.Variables.Add(new VariableDefinition("__methodInfo", module.Import(typeof(MethodInfo))));
            method.Body.Variables.Add(new VariableDefinition("__methodExecutionArgs",
                                                             module.Import(typeof(MethodExecutionArgs))));
            method.Body.Variables.Add(new VariableDefinition("__ex", module.Import(typeof(Exception))));

            var argsVariableRef = new VariableDefinition("__args", module.Import(typeof(object[])));
            method.Body.Variables.Add(argsVariableRef);

            var boolVariableRef = new VariableDefinition("__bool", module.Import(typeof(bool)));
            method.Body.Variables.Add(boolVariableRef);

            var flowBehaviorVariableRef = new VariableDefinition("__flow", module.Import(typeof(FlowBehavior)));
            method.Body.Variables.Add(flowBehaviorVariableRef);

            Instruction L_0032_Instruction;
            Instruction firstAfterInstruction = Instruction.Create(OpCodes.Nop);

            const int Variable_aspect = 0;
            const int Variable_methodInfo = 1;
            const int Variable_methodExecutionArgs = 2;
            const int Variable_ex = 3;

            var beforeInstructions = new List<Instruction>
                {
                    processor.Create(OpCodes.Nop),
                    processor.Create(OpCodes.Newobj, aspectConstructorRef),
                    processor.Create(OpCodes.Stloc, Variable_aspect + existingVariableCount),
                    processor.Create(OpCodes.Call, getCurrentMethodRef),
                    processor.Create(OpCodes.Isinst, methodInfoRef),
                    processor.Create(OpCodes.Stloc, Variable_methodInfo + existingVariableCount),
                    processor.Create(OpCodes.Ldloc, Variable_methodInfo + existingVariableCount),
                };

            beforeInstructions.AddRange(new List<Instruction> {
                    processor.Create(OpCodes.Ldc_I4, method.Parameters.Count), // Number of arguments in method
                    processor.Create(OpCodes.Newarr, objectRef), // Create the object array (sized to hold args)
                    processor.Create(OpCodes.Stloc_S, argsVariableRef), // Save array to variable
                    processor.Create(OpCodes.Ldloc_S, argsVariableRef), // Load variable for evaluation
            });

            for (int i = 0; i < method.Parameters.Count; i++)
            {
                beforeInstructions.Add(processor.Create(OpCodes.Ldc_I4, i));  // Index value 0
                beforeInstructions.Add(processor.Create(OpCodes.Ldarg, i + 1)); // Load first argument

                var parameterType = method.Parameters[i].ParameterType;

                if (parameterType.IsValueType)
                    beforeInstructions.Add(processor.Create(OpCodes.Box, parameterType)); // Box the value types

                beforeInstructions.Add(processor.Create(OpCodes.Stelem_Ref)); // Assign argument to index 0 of array
                beforeInstructions.Add(processor.Create(OpCodes.Ldloc_S, argsVariableRef));
            }

            beforeInstructions.AddRange(new List<Instruction> {
                    //processor.Create(OpCodes.Ldc_I4, 2), // Number of arguments in method
                    //processor.Create(OpCodes.Newarr, objectRef), // Create the object array (sized to hold args)
                    //processor.Create(OpCodes.Stloc_S, argsVariableRef), // Save array to variable
                    //processor.Create(OpCodes.Ldloc_S, argsVariableRef), // Load variable for evaluation

                    //processor.Create(OpCodes.Ldc_I4_0), // Index value 0
                    //processor.Create(OpCodes.Ldarg_1), // Load first argument
                    //processor.Create(OpCodes.Stelem_Ref), // Assign argument to index 0 of array
                    //processor.Create(OpCodes.Ldloc_S, argsVariableRef),

                    //processor.Create(OpCodes.Ldc_I4_1), // Index value 1
                    //processor.Create(OpCodes.Ldarg_2), // Second argument
                    //processor.Create(OpCodes.Box, int32Ref), // Box the value types
                    //processor.Create(OpCodes.Stelem_Ref), // Assign argument to index 1 of array
                    //processor.Create(OpCodes.Ldloc_S, argsVariableRef),
                    
                    processor.Create(OpCodes.Newobj, methodExecutionArgsCtorRef),
                    processor.Create(OpCodes.Stloc, Variable_methodExecutionArgs + existingVariableCount),
                    (L_0032_Instruction = processor.Create(OpCodes.Nop)),
                    processor.Create(OpCodes.Ldloc, Variable_aspect + existingVariableCount),
                    processor.Create(OpCodes.Ldloc, Variable_methodExecutionArgs + existingVariableCount),
                    processor.Create(OpCodes.Callvirt, onEntryRef),
                    processor.Create(OpCodes.Nop),
                    processor.Create(OpCodes.Ldloc, Variable_methodExecutionArgs + existingVariableCount),
                    processor.Create(OpCodes.Callvirt, getFlowBehaviorRef),
                    processor.Create(OpCodes.Ldc_I4_3),
                    processor.Create(OpCodes.Ceq),
                    processor.Create(OpCodes.Ldc_I4_0),
                    processor.Create(OpCodes.Ceq),
                    processor.Create(OpCodes.Stloc_S, boolVariableRef),
                    processor.Create(OpCodes.Ldloc_S, boolVariableRef),
                    processor.Create(OpCodes.Brtrue_S, firstInstruction),
                    processor.Create(OpCodes.Leave, finalInstruction),
                });

            var L_0094_Instruction = processor.Create(OpCodes.Rethrow);
            var L_00a2_Instruction = processor.Create(OpCodes.Nop);
            var L_00b0_Instruction = processor.Create(OpCodes.Nop);
            var L_00b1_Instruction = processor.Create(OpCodes.Nop);
            var L_0096_Instruction = processor.Create(OpCodes.Leave_S, L_00b1_Instruction);
            var L_009f_Instruction = processor.Create(OpCodes.Nop);
            var L_0098_Instruction = processor.Create(OpCodes.Ldloc, 2 + existingVariableCount);

            Instruction L_005d_Instruction;
            Instruction L_00a5_Instruction;

            var afterInstructions = new List<Instruction>
                {
                    firstAfterInstruction,
                    processor.Create(OpCodes.Ldloc, Variable_aspect + existingVariableCount),
                    processor.Create(OpCodes.Ldloc, Variable_methodExecutionArgs + existingVariableCount),
                    processor.Create(OpCodes.Callvirt, onSuccessRef),
                    processor.Create(OpCodes.Nop),
                    processor.Create(OpCodes.Nop),
                    processor.Create(OpCodes.Leave_S, L_00a2_Instruction),
                    (L_005d_Instruction = processor.Create(OpCodes.Stloc, Variable_ex + existingVariableCount)),
                    processor.Create(OpCodes.Nop),
                    processor.Create(OpCodes.Ldloc, Variable_methodExecutionArgs + existingVariableCount),
                    processor.Create(OpCodes.Ldloc, Variable_ex + existingVariableCount),
                    processor.Create(OpCodes.Callvirt, setExceptionRef),
                    processor.Create(OpCodes.Nop),
                    processor.Create(OpCodes.Ldloc, Variable_aspect + existingVariableCount),
                    processor.Create(OpCodes.Ldloc, Variable_methodExecutionArgs + existingVariableCount),
                    processor.Create(OpCodes.Callvirt, onExceptionRef),
                    processor.Create(OpCodes.Nop),
                    processor.Create(OpCodes.Ldloc, Variable_methodExecutionArgs + existingVariableCount),
                    processor.Create(OpCodes.Callvirt, getFlowBehaviorRef),
                    processor.Create(OpCodes.Stloc_S, flowBehaviorVariableRef),
                    processor.Create(OpCodes.Ldloc_S, flowBehaviorVariableRef),
                    processor.Create(OpCodes.Switch,
                                     new[]
                                         {
                                             L_0094_Instruction, L_0096_Instruction, L_0094_Instruction, L_009f_Instruction,
                                             L_0098_Instruction
                                         }),
                    processor.Create(OpCodes.Br_S, L_009f_Instruction),
                    L_0094_Instruction,
                    L_0096_Instruction,
                    L_0098_Instruction,
                    processor.Create(OpCodes.Callvirt, getExceptionRef),
                    processor.Create(OpCodes.Throw),
                    L_009f_Instruction,
                    processor.Create(OpCodes.Leave_S, L_00a2_Instruction),
                    L_00a2_Instruction,
                    processor.Create(OpCodes.Leave_S, L_00b0_Instruction),
                    (L_00a5_Instruction = processor.Create(OpCodes.Nop)),
                    processor.Create(OpCodes.Ldloc, Variable_aspect + existingVariableCount),
                    processor.Create(OpCodes.Ldloc, Variable_methodExecutionArgs + existingVariableCount),
                    processor.Create(OpCodes.Callvirt, onExitRef), // L_00a8
                    processor.Create(OpCodes.Nop),
                    processor.Create(OpCodes.Nop),
                    processor.Create(OpCodes.Endfinally),
                    L_00b0_Instruction,
                    L_00b1_Instruction,
                };

            var catchHandler = new ExceptionHandler(ExceptionHandlerType.Catch)
            {
                TryStart = L_0032_Instruction,
                TryEnd = L_005d_Instruction,
                HandlerStart = L_005d_Instruction,
                HandlerEnd = L_00a2_Instruction,
                CatchType = module.Import(typeof(Exception)),
            };

            var finallyHandler = new ExceptionHandler(ExceptionHandlerType.Finally)
            {
                TryStart = L_0032_Instruction,
                TryEnd = L_00a5_Instruction,
                HandlerStart = L_00a5_Instruction,
                HandlerEnd = L_00b0_Instruction,
            };

            // Add the OnEntry portion
            foreach (var instruction in beforeInstructions)
                processor.InsertBefore(firstInstruction, instruction);

            // Add the OnExit portion
            foreach (var instruction in afterInstructions)
                processor.InsertBefore(finalInstruction, instruction);

            //// Add try/catch
            method.Body.ExceptionHandlers.Add(catchHandler);
            method.Body.ExceptionHandlers.Add(finallyHandler);

            method.Body.OptimizeMacros();
        }
Exemplo n.º 9
0
 static private bool IsDefined(MemberInfo member, Type caType, CustomAttribute caItem) {
     MemberTypes memberType = member.MemberType;
     int token = CustomAttribute.GetMemberToken(member, memberType);
     IntPtr module = CustomAttribute.GetMemberModule(member, memberType);
     return CustomAttribute.IsCADefinedCheckType(caType, module, token);
 }
Exemplo n.º 10
0
 /////////////////////////// ICustomAttributeProvider Interface
 // Return an array of all of the custom attributes
 public override Object[] GetCustomAttributes(bool inherit)
 {
     return(CustomAttribute.GetCustomAttributes(this, null, inherit));
 }
Exemplo n.º 11
0
 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
 {
     return(CustomAttribute.GetCustomAttributes(this, attributeType, false));
 }
Exemplo n.º 12
0
 // According to MSDN the inherit parameter is ignored here and
 // the behavior always defaults to inherit = false
 //
 public override bool IsDefined(Type attributeType, bool inherit)
 {
     return(CustomAttribute.IsDefined(this, attributeType, false));
 }
Exemplo n.º 13
0
        private bool IsNonW8PFrameworkAPI()
        {
            if (this.DeclaringType.IsArray && this.IsPublic && !this.IsStatic)
            {
                return(false);
            }
            RuntimeAssembly runtimeAssembly = this.GetRuntimeAssembly();

            if (runtimeAssembly.IsFrameworkAssembly())
            {
                int attributeCtorToken = runtimeAssembly.InvocableAttributeCtorToken;
                if (System.Reflection.MetadataToken.IsNullToken(attributeCtorToken) || !CustomAttribute.IsAttributeDefined(this.GetRuntimeModule(), this.MetadataToken, attributeCtorToken))
                {
                    return(true);
                }
            }
            return(this.GetRuntimeType().IsNonW8PFrameworkAPI());
        }
		CustomAttribute ConvertAttribute (object ob)
		{
			CustomAttribute at = new CustomAttribute ();
			Type type = ob.GetType ();
			at.TypeName = type.FullName;
			
			foreach (PropertyInfo prop in type.GetProperties (BindingFlags.Public | BindingFlags.Instance)) {
				object val = prop.GetValue (ob, null);
				if (val != null) {
					NodeAttributeAttribute bt = (NodeAttributeAttribute) Attribute.GetCustomAttribute (prop, typeof(NodeAttributeAttribute), true);
					if (bt != null) {
						string name = string.IsNullOrEmpty (bt.Name) ? prop.Name : bt.Name;
						at [name] = Convert.ToString (val, System.Globalization.CultureInfo.InvariantCulture);
					}
				}
			}
			foreach (FieldInfo field in type.GetFields (BindingFlags.Public | BindingFlags.Instance)) {
				object val = field.GetValue (ob);
				if (val != null) {
					NodeAttributeAttribute bt = (NodeAttributeAttribute) Attribute.GetCustomAttribute (field, typeof(NodeAttributeAttribute), true);
					if (bt != null) {
						string name = string.IsNullOrEmpty (bt.Name) ? field.Name : bt.Name;
						at [name] = Convert.ToString (val, System.Globalization.CultureInfo.InvariantCulture);
					}
				}
			}
			return at;
		}
Exemplo n.º 15
0
 public override object[] GetCustomAttributes(bool inherit)
 {
     return CustomAttribute.GetCustomAttributes(this, inherit);
 }
Exemplo n.º 16
0
 object[] GetCustomAttributes(bool inherit)
 {
     return(CustomAttribute.GetCustomAttributes(this, inherit));
 }
Exemplo n.º 17
0
 object[] GetCustomAttributes(Type attributeType, bool inherit)
 {
     return(CustomAttribute.GetCustomAttributes(this, attributeType, inherit));
 }
Exemplo n.º 18
0
 static private CustomAttribute GetCustomAttributeList(MemberInfo member, Type caType, CustomAttribute caItem, int level) {
     MemberTypes memberType = member.MemberType;
     int token = CustomAttribute.GetMemberToken(member, memberType);
     IntPtr module = CustomAttribute.GetMemberModule(member, memberType);
     return CustomAttribute.GetCustomAttributeListCheckType(token, module, caType, caItem, level);
 }
Exemplo n.º 19
0
 ExtensionNodeDescription AddCustomAttributeExtension(ModuleDescription module, CustomAttribute att, string nameName)
 {
     string path;
     if (!att.TryGetValue (CustomExtensionAttribute.PathFieldKey, out path))
         path = "%" + att.TypeName;
     ExtensionNodeDescription elem = module.AddExtensionNode (path, nameName);
     foreach (KeyValuePair<string,string> prop in att) {
         if (prop.Key != CustomExtensionAttribute.PathFieldKey)
             elem.SetAttribute (prop.Key, prop.Value);
     }
     return elem;
 }
Exemplo n.º 20
0
        // does some consisitency check between the CA list and every CA attribute usage and after
        // discarding unwanted CA return an array of them
        static private Object[] CheckConsistencyAndCreateArray(CustomAttribute caItem, Type caType) {
            // we got the max number of attributes, we may have to trim this list but let's get a count for now
            if (caItem == null) 
                return (caType == null || caType.IsValueType) ? s_gObjectEmptyArray : (Object[])Array.CreateInstance(caType, 0);
            
            int caCount = 0;
            int hasInherited = (caItem == null) ? 0 : caItem.m_inheritLevel;
            CustomAttribute caNext = caItem;
            CustomAttribute caPrev = null;
            // walk and revert the list
            while (caNext != null) {
                caCount++;
                CustomAttribute caTemp = caNext.m_next;
                caNext.m_next = caPrev;
                caPrev = caNext;
                caNext = caTemp;
            }
            caItem = caPrev;
            
            // now we have a list of custom attribute of some type. That list may contain subtype of the specific
            // type, so for every other type we have to go and check whether multiple and inherited are respected
            if (caCount > 0 && hasInherited > 0) {

                // keep track of the CA types so we can make inspection reasonable fast
                Hashtable htCAData = new Hashtable(11);

                // loop through the list of attributes checking for consistency, once an attribute
                // has been inspected is never going to be looked at again, and neither an attribute 
                // of the same type. 
                // This loop and the inspectedCATypes array should make that happen
                caNext = caItem;
                CustomAttribute caLast = null;
                while (caNext != null) {
                    AttributeUsageAttribute usage = null;
                    int caTypeLevelFound = 0;
                    
                    // get current attribute type
                    Type t = caNext.GetAttributeType();
                    
                    // look if the type has already been seen
                    CAData caData = (CAData)htCAData[t];
                    if (caData != null) {
                        usage = caData.usage;
                        caTypeLevelFound = caData.level;
                    }
                    
                    if (usage == null) 
                        // no type found, load the attribute usage 
                        // do not save the attribute usage yet because the next block may discard 
                        // the attribute which implies we pretend we never saw. That is kind of
                        // bad because we may end up reloading it again if another type comes into the picture 
                        // but we prefer the perf hit to more code complication for a case that seem to
                        // be submarginal
                        usage = CustomAttribute.GetAttributeUsage(t);

                    // if this is an inherited attribute and the attribute usage does not allow that, OR
                    // if the attribute was seen already on a different inheritance level and multiple is not allowed
                    // THEN discard
                    if ((caNext.m_inheritLevel > 0 && !usage.Inherited) || 
                            (caData != null && caTypeLevelFound < caNext.m_inheritLevel && !usage.AllowMultiple)) {
                        if (caLast == null) 
                            caItem = caNext.m_next;
                        else 
                            caLast.m_next = caNext.m_next;
                        caNext = caNext.m_next;
                        caCount--;
                        continue;
                    }

                    if (caData == null) 
                        // the attribute was never seen so far so we should add it to the table and keep going
                        htCAData[t] = new CAData(t, usage, caNext.m_inheritLevel);

                    caLast = caNext;
                    caNext = caNext.m_next;
                }
            }

            // time to return the array
            if (caCount > 0) {
                if (caType == null || caType.IsValueType) 
                    caType = s_ObjectType;
                Object[] cas = (Object[])Array.CreateInstance(caType, caCount);
                caNext = caItem;
                for (int i = 0; i < caCount; i++) {
                    cas[i] = caNext.GetObject();
                    caNext = caNext.m_next;
                }
                return cas;
            }
            else
                return (caType == null || caType.IsValueType) ? s_gObjectEmptyArray : (Object[])Array.CreateInstance(caType, 0);
        }
Exemplo n.º 21
0
 static private CustomAttribute GetCustomAttributeListCheckType(int token, IntPtr module, Type caType, CustomAttribute caItem, int level) {
     if (caType != null) {
         caType = caType.UnderlyingSystemType;
         if (!(caType is RuntimeType)) 
 			throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"caType");
     }
     return GetCustomAttributeList(token, module, ((RuntimeType)caType), caItem, level);
 }
Exemplo n.º 22
0
 internal static IList <CustomAttributeData> GetCustomAttributesInternal(RuntimeAssembly target)
 {
     return(CustomAttribute.GetCustomAttributesData(target));
 }