void ReadSecurityBlob(BlobReader reader, IAttribute[] attributes, ITypeResolveContext context, ResolveResult securityActionRR)
        {
            for (int i = 0; i < attributes.Length; i++)
            {
                string         attributeTypeName = reader.ReadSerString();
                ITypeReference attributeTypeRef  = ReflectionHelper.ParseReflectionName(attributeTypeName);
                IType          attributeType     = attributeTypeRef.Resolve(context);

                reader.ReadCompressedUInt32();                 // ??
                // The specification seems to be incorrect here, so I'm using the logic from Cecil instead.
                uint numNamed = reader.ReadCompressedUInt32();

                var namedArgs = new List <KeyValuePair <IMember, ResolveResult> >((int)numNamed);
                for (uint j = 0; j < numNamed; j++)
                {
                    var namedArg = reader.ReadNamedArg(attributeType);
                    if (namedArg.Key != null)
                    {
                        namedArgs.Add(namedArg);
                    }
                }
                attributes[i] = new DefaultAttribute(
                    attributeType,
                    positionalArguments: new ResolveResult[] { securityActionRR },
                    namedArguments: namedArgs);
            }
        }
Пример #2
0
        bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
        {
            DefaultAttribute a = other as DefaultAttribute;

            return(a != null && attributeType == a.attributeType && positionalArguments == a.positionalArguments && namedArguments == a.namedArguments && region == a.region);
        }
Пример #3
0
		public IAttribute ReadAttribute(CustomAttribute attribute)
		{
			if (attribute == null)
				throw new ArgumentNullException("attribute");
			MethodReference ctor = attribute.Constructor;
			ITypeReference[] ctorParameters = null;
			if (ctor.HasParameters) {
				ctorParameters = new ITypeReference[ctor.Parameters.Count];
				for (int i = 0; i < ctorParameters.Length; i++) {
					ctorParameters[i] = ReadTypeReference(ctor.Parameters[i].ParameterType);
				}
			}
			DefaultAttribute a = new DefaultAttribute(ReadTypeReference(attribute.AttributeType), ctorParameters);
			try {
				if (attribute.HasConstructorArguments) {
					foreach (var arg in attribute.ConstructorArguments) {
						a.PositionalArguments.Add(ReadConstantValue(arg));
					}
				}
			} catch (InvalidOperationException) {
				// occurs when Cecil can't decode an argument
			}
			try {
				if (attribute.HasFields || attribute.HasProperties) {
					foreach (var arg in attribute.Fields) {
						a.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(arg.Name, ReadConstantValue(arg.Argument)));
					}
					foreach (var arg in attribute.Properties) {
						a.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(arg.Name, ReadConstantValue(arg.Argument)));
					}
				}
			} catch (InvalidOperationException) {
				// occurs when Cecil can't decode an argument
			}
			return a;
		}
Пример #4
0
		void AddAttributes(FieldDefinition fieldDefinition, IEntity targetEntity)
		{
			#region FieldOffsetAttribute
			if (fieldDefinition.HasLayoutInfo) {
				DefaultAttribute fieldOffset = new DefaultAttribute(fieldOffsetAttributeTypeRef, new[] { KnownTypeReference.Int32 });
				fieldOffset.PositionalArguments.Add(new SimpleConstantValue(KnownTypeReference.Int32, fieldDefinition.Offset));
				targetEntity.Attributes.Add(fieldOffset);
			}
			#endregion
			
			#region NonSerializedAttribute
			if (fieldDefinition.IsNotSerialized) {
				targetEntity.Attributes.Add(nonSerializedAttribute);
			}
			#endregion
			
			if (fieldDefinition.HasMarshalInfo) {
				targetEntity.Attributes.Add(ConvertMarshalInfo(fieldDefinition.MarshalInfo));
			}
			
			if (fieldDefinition.HasCustomAttributes) {
				AddCustomAttributes(fieldDefinition.CustomAttributes, targetEntity.Attributes);
			}
		}
Пример #5
0
		static IAttribute ConvertMarshalInfo(MarshalInfo marshalInfo)
		{
			DefaultAttribute attr = new DefaultAttribute(marshalAsAttributeTypeRef, new[] { unmanagedTypeTypeRef });
			attr.PositionalArguments.Add(new SimpleConstantValue(unmanagedTypeTypeRef, (int)marshalInfo.NativeType));
			// TODO: handle classes derived from MarshalInfo
			return attr;
		}
Пример #6
0
		void AddAttributes(TypeDefinition typeDefinition, ITypeDefinition targetEntity)
		{
			#region SerializableAttribute
			if (typeDefinition.IsSerializable)
				targetEntity.Attributes.Add(serializableAttribute);
			#endregion
			
			#region StructLayoutAttribute
			LayoutKind layoutKind = LayoutKind.Auto;
			switch (typeDefinition.Attributes & TypeAttributes.LayoutMask) {
				case TypeAttributes.SequentialLayout:
					layoutKind = LayoutKind.Sequential;
					break;
				case TypeAttributes.ExplicitLayout:
					layoutKind = LayoutKind.Explicit;
					break;
			}
			CharSet charSet = CharSet.None;
			switch (typeDefinition.Attributes & TypeAttributes.StringFormatMask) {
				case TypeAttributes.AnsiClass:
					charSet = CharSet.Ansi;
					break;
				case TypeAttributes.AutoClass:
					charSet = CharSet.Auto;
					break;
				case TypeAttributes.UnicodeClass:
					charSet = CharSet.Unicode;
					break;
			}
			LayoutKind defaultLayoutKind = (typeDefinition.IsValueType && !typeDefinition.IsEnum) ? LayoutKind.Sequential: LayoutKind.Auto;
			if (layoutKind != defaultLayoutKind || charSet != CharSet.Ansi || typeDefinition.PackingSize > 0 || typeDefinition.ClassSize > 0) {
				DefaultAttribute structLayout = new DefaultAttribute(structLayoutAttributeTypeRef, new[] { layoutKindTypeRef });
				structLayout.PositionalArguments.Add(new SimpleConstantValue(layoutKindTypeRef, (int)layoutKind));
				if (charSet != CharSet.Ansi) {
					structLayout.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(
						"CharSet",
						new SimpleConstantValue(charSetTypeRef, (int)charSet)));
				}
				if (typeDefinition.PackingSize > 0) {
					structLayout.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(
						"Pack",
						new SimpleConstantValue(KnownTypeReference.Int32, (int)typeDefinition.PackingSize)));
				}
				if (typeDefinition.ClassSize > 0) {
					structLayout.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(
						"Size",
						new SimpleConstantValue(KnownTypeReference.Int32, (int)typeDefinition.ClassSize)));
				}
				targetEntity.Attributes.Add(structLayout);
			}
			#endregion
			
			if (typeDefinition.HasCustomAttributes) {
				AddCustomAttributes(typeDefinition.CustomAttributes, targetEntity.Attributes);
			}
		}
Пример #7
0
		static void AddNamedArgument(DefaultAttribute attribute, string name, IConstantValue value)
		{
			attribute.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(name, value));
		}
Пример #8
0
		void AddAttributes(MethodDefinition methodDefinition, IList<IAttribute> attributes, IList<IAttribute> returnTypeAttributes)
		{
			MethodImplAttributes implAttributes = methodDefinition.ImplAttributes & ~MethodImplAttributes.CodeTypeMask;
			
			#region DllImportAttribute
			if (methodDefinition.HasPInvokeInfo) {
				PInvokeInfo info = methodDefinition.PInvokeInfo;
				DefaultAttribute dllImport = new DefaultAttribute(dllImportAttributeTypeRef, new[] { KnownTypeReference.String });
				dllImport.PositionalArguments.Add(new SimpleConstantValue(KnownTypeReference.String, info.Module.Name));
				
				if (info.IsBestFitDisabled)
					AddNamedArgument(dllImport, "BestFitMapping", falseValue);
				if (info.IsBestFitEnabled)
					AddNamedArgument(dllImport, "BestFitMapping", trueValue);
				
				CallingConvention callingConvention;
				switch (info.Attributes & PInvokeAttributes.CallConvMask) {
					case PInvokeAttributes.CallConvCdecl:
						callingConvention = CallingConvention.Cdecl;
						break;
					case PInvokeAttributes.CallConvFastcall:
						callingConvention = CallingConvention.FastCall;
						break;
					case PInvokeAttributes.CallConvStdCall:
						callingConvention = CallingConvention.StdCall;
						break;
					case PInvokeAttributes.CallConvThiscall:
						callingConvention = CallingConvention.ThisCall;
						break;
					case PInvokeAttributes.CallConvWinapi:
						callingConvention = CallingConvention.Winapi;
						break;
					default:
						throw new NotSupportedException("unknown calling convention");
				}
				if (callingConvention != CallingConvention.Winapi)
					AddNamedArgument(dllImport, "CallingConvention", new SimpleConstantValue(callingConventionTypeRef, (int)callingConvention));
				
				CharSet charSet = CharSet.None;
				switch (info.Attributes & PInvokeAttributes.CharSetMask) {
					case PInvokeAttributes.CharSetAnsi:
						charSet = CharSet.Ansi;
						break;
					case PInvokeAttributes.CharSetAuto:
						charSet = CharSet.Auto;
						break;
					case PInvokeAttributes.CharSetUnicode:
						charSet = CharSet.Unicode;
						break;
				}
				if (charSet != CharSet.None)
					dllImport.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(
						"CharSet", new SimpleConstantValue(charSetTypeRef, (int)charSet)));
				
				if (!string.IsNullOrEmpty(info.EntryPoint) && info.EntryPoint != methodDefinition.Name)
					AddNamedArgument(dllImport, "EntryPoint", new SimpleConstantValue(KnownTypeReference.String, info.EntryPoint));
				
				if (info.IsNoMangle)
					AddNamedArgument(dllImport, "ExactSpelling", trueValue);
				
				if ((implAttributes & MethodImplAttributes.PreserveSig) == MethodImplAttributes.PreserveSig)
					implAttributes &= ~MethodImplAttributes.PreserveSig;
				else
					AddNamedArgument(dllImport, "PreserveSig", falseValue);
				
				if (info.SupportsLastError)
					AddNamedArgument(dllImport, "SetLastError", trueValue);
				
				if (info.IsThrowOnUnmappableCharDisabled)
					AddNamedArgument(dllImport, "ThrowOnUnmappableChar", falseValue);
				if (info.IsThrowOnUnmappableCharEnabled)
					AddNamedArgument(dllImport, "ThrowOnUnmappableChar", trueValue);
				
				attributes.Add(dllImport);
			}
			#endregion
			
			#region PreserveSigAttribute
			if (implAttributes == MethodImplAttributes.PreserveSig) {
				attributes.Add(preserveSigAttribute);
				implAttributes = 0;
			}
			#endregion
			
			#region MethodImplAttribute
			if (implAttributes != 0) {
				DefaultAttribute methodImpl = new DefaultAttribute(methodImplAttributeTypeRef, new[] { methodImplOptionsTypeRef });
				methodImpl.PositionalArguments.Add(new SimpleConstantValue(methodImplOptionsTypeRef, (int)implAttributes));
				attributes.Add(methodImpl);
			}
			#endregion
			
			if (methodDefinition.HasCustomAttributes) {
				AddCustomAttributes(methodDefinition.CustomAttributes, attributes);
			}
			if (methodDefinition.MethodReturnType.HasMarshalInfo) {
				returnTypeAttributes.Add(ConvertMarshalInfo(methodDefinition.MethodReturnType.MarshalInfo));
			}
			if (methodDefinition.MethodReturnType.HasCustomAttributes) {
				AddCustomAttributes(methodDefinition.MethodReturnType.CustomAttributes, returnTypeAttributes);
			}
		}
Пример #9
0
 public IAttribute ReadAttribute(CustomAttribute attribute)
 {
     if (attribute == null)
         throw new ArgumentNullException("attribute");
     DefaultAttribute a = new DefaultAttribute(ReadTypeReference(attribute.AttributeType));
     try {
         if (attribute.HasConstructorArguments) {
             foreach (var arg in attribute.ConstructorArguments) {
                 a.PositionalArguments.Add(ReadConstantValue(arg));
             }
         }
     } catch (InvalidOperationException) {
         // occurs when Cecil can't decode an argument
     }
     try {
         if (attribute.HasFields || attribute.HasProperties) {
             foreach (var arg in attribute.Fields) {
                 a.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(arg.Name, ReadConstantValue(arg.Argument)));
             }
             foreach (var arg in attribute.Properties) {
                 a.NamedArguments.Add(new KeyValuePair<string, IConstantValue>(arg.Name, ReadConstantValue(arg.Argument)));
             }
         }
     } catch (InvalidOperationException) {
         // occurs when Cecil can't decode an argument
     }
     return a;
 }
Пример #10
0
            void ReadSecurityBlob(BlobReader reader, IAttribute[] attributes, ITypeResolveContext context, ResolveResult securityActionRR)
            {
                for (int i = 0; i < attributes.Length; i++) {
                    string attributeTypeName = reader.ReadSerString();
                    ITypeReference attributeTypeRef = ReflectionHelper.ParseReflectionName(attributeTypeName);
                    IType attributeType = attributeTypeRef.Resolve(context);

                    reader.ReadCompressedUInt32(); // ??
                    // The specification seems to be incorrect here, so I'm using the logic from Cecil instead.
                    uint numNamed = reader.ReadCompressedUInt32();

                    var namedArgs = new List<KeyValuePair<IMember, ResolveResult>>((int)numNamed);
                    for (uint j = 0; j < numNamed; j++) {
                        var namedArg = reader.ReadNamedArg(attributeType);
                        if (namedArg.Key != null)
                            namedArgs.Add(namedArg);

                    }
                    attributes[i] = new DefaultAttribute(
                        attributeType,
                        positionalArguments: new ResolveResult[] { securityActionRR },
                    namedArguments: namedArgs);
                }
            }