public byte [] CompressCustomAttribute(CustomAttrib ca, MethodReference ctor)
        {
            MemoryBinaryWriter writer = new MemoryBinaryWriter();

            CompressCustomAttribute(ca, ctor, writer);
            return(writer.ToArray());
        }
        void Write(CustomAttrib ca, MethodReference ctor, MemoryBinaryWriter writer)
        {
            if (ca == null)
            {
                return;
            }

            if (ca.Prolog != CustomAttrib.StdProlog)
            {
                return;
            }

            writer.Write(ca.Prolog);

            for (int i = 0; i < ctor.Parameters.Count; i++)
            {
                Write(ca.FixedArgs [i], writer);
            }

            writer.Write(ca.NumNamed);

            for (int i = 0; i < ca.NumNamed; i++)
            {
                Write(ca.NamedArgs [i], writer);
            }
        }
        CustomAttrib ReadCustomAttrib(int pos, MethodReference ctor)
        {
            int start, length = Utilities.ReadCompressedInteger(m_blobData, pos, out start);

            byte [] data = new byte [length];
            Buffer.BlockCopy(m_blobData, start, data, 0, length);
            try {
                return(ReadCustomAttrib(new BinaryReader(
                                            new MemoryStream(data)), data, ctor));
            } catch {
                CustomAttrib ca = new CustomAttrib(ctor);
                ca.Read = false;
                return(ca);
            }
        }
        CustomAttrib ReadCustomAttrib(BinaryReader br, byte [] data, MethodReference ctor)
        {
            CustomAttrib ca = new CustomAttrib(ctor);

            if (data.Length == 0)
            {
                ca.FixedArgs = new CustomAttrib.FixedArg [0];
                ca.NamedArgs = new CustomAttrib.NamedArg [0];
                return(ca);
            }

            bool read = true;

            ca.Prolog = br.ReadUInt16();
            if (ca.Prolog != CustomAttrib.StdProlog)
            {
                throw new MetadataFormatException("Non standard prolog for custom attribute");
            }

            ca.FixedArgs = new CustomAttrib.FixedArg [ctor.Parameters.Count];
            for (int i = 0; i < ca.FixedArgs.Length && read; i++)
            {
                ca.FixedArgs [i] = ReadFixedArg(data, br, ctor.Parameters [i].ParameterType is ArrayType,
                                                ctor.Parameters [i].ParameterType, ref read);
            }

            if (br.BaseStream.Position == br.BaseStream.Length)
            {
                read = false;
            }

            if (!read)
            {
                ca.Read = read;
                return(ca);
            }

            ca.NumNamed  = br.ReadUInt16();
            ca.NamedArgs = new CustomAttrib.NamedArg [ca.NumNamed];
            for (int i = 0; i < ca.NumNamed && read; i++)
            {
                ca.NamedArgs [i] = ReadNamedArg(data, br, ref read);
            }

            ca.Read = read;
            return(ca);
        }
Beispiel #5
0
		public uint AddCustomAttribute (CustomAttrib ca, MethodReference ctor)
		{
			CompressCustomAttribute (ca, ctor, m_sigWriter);
			return GetPointer ();
		}
        public CustomAttrib GetCustomAttributeSig(CustomAttribute ca)
        {
            CustomAttrib cas = new CustomAttrib (ca.Constructor);
            cas.Prolog = CustomAttrib.StdProlog;

            cas.FixedArgs = new CustomAttrib.FixedArg [ca.Constructor.Parameters.Count];

            for (int i = 0; i < cas.FixedArgs.Length; i++)
                cas.FixedArgs [i] = CreateFixedArg (
                    ca.Constructor.Parameters [i].ParameterType, ca.ConstructorParameters [i]);

            int nn = ca.Fields.Count + ca.Properties.Count;
            cas.NumNamed = (ushort) nn;
            cas.NamedArgs = new CustomAttrib.NamedArg [nn];

            if (cas.NamedArgs.Length > 0) {
                int curs = 0;
                foreach (DictionaryEntry entry in ca.Fields) {
                    string field = (string) entry.Key;
                    cas.NamedArgs [curs++] = CreateNamedArg (
                        ca.GetFieldType (field), field, entry.Value, true);
                }

                foreach (DictionaryEntry entry in ca.Properties) {
                    string property = (string) entry.Key;
                    cas.NamedArgs [curs++] = CreateNamedArg (
                        ca.GetPropertyType (property), property, entry.Value, false);
                }
            }

            return cas;
        }
Beispiel #7
0
 static object GetFixedArgValue(CustomAttrib.FixedArg fa)
 {
     if (fa.SzArray) {
         object [] vals = new object [fa.NumElem];
         for (int j = 0; j < vals.Length; j++)
             vals [j] = fa.Elems [j].Value;
         return vals;
     } else
         return fa.Elems [0].Value;
 }
Beispiel #8
0
        protected CustomAttribute BuildCustomAttribute(MethodReference ctor, CustomAttrib sig)
        {
            CustomAttribute cattr = new CustomAttribute (ctor);

            foreach (CustomAttrib.FixedArg fa in sig.FixedArgs)
                cattr.ConstructorParameters.Add (GetFixedArgValue (fa));

            foreach (CustomAttrib.NamedArg na in sig.NamedArgs) {
                object value = GetFixedArgValue (na.FixedArg);
                if (na.Field) {
                    cattr.Fields [na.FieldOrPropName] = value;
                    cattr.SetFieldType (na.FieldOrPropName, GetNamedArgType (na));
                } else if (na.Property) {
                    cattr.Properties [na.FieldOrPropName] = value;
                    cattr.SetPropertyType (na.FieldOrPropName, GetNamedArgType (na));
                } else
                    throw new ReflectionException ("Non valid named arg");
            }

            return cattr;
        }
Beispiel #9
0
        CustomAttrib ReadCustomAttrib(BinaryReader br, byte [] data, MethodReference ctor, bool resolve)
        {
            CustomAttrib ca = new CustomAttrib (ctor);
            if (data.Length == 0) {
                ca.FixedArgs = new CustomAttrib.FixedArg [0];
                ca.NamedArgs = new CustomAttrib.NamedArg [0];
                return ca;
            }

            bool read = true;

            ca.Prolog = br.ReadUInt16 ();
            if (ca.Prolog != CustomAttrib.StdProlog)
                throw new MetadataFormatException ("Non standard prolog for custom attribute");

            ca.FixedArgs = new CustomAttrib.FixedArg [ctor.Parameters.Count];
            for (int i = 0; i < ca.FixedArgs.Length && read; i++)
                ca.FixedArgs [i] = ReadFixedArg (data, br,
                    ctor.Parameters [i].ParameterType, ref read, resolve);

            if (br.BaseStream.Position == br.BaseStream.Length)
                read = false;

            if (!read) {
                ca.Read = read;
                return ca;
            }

            ca.NumNamed = br.ReadUInt16 ();
            ca.NamedArgs = new CustomAttrib.NamedArg [ca.NumNamed];
            for (int i = 0; i < ca.NumNamed && read; i++)
                ca.NamedArgs [i] = ReadNamedArg (data, br, ref read, resolve);

            ca.Read = read;
            return ca;
        }
Beispiel #10
0
		void Write (CustomAttrib.NamedArg na, MemoryBinaryWriter writer)
		{
			if (na.Field)
				writer.Write ((byte) 0x53);
			else if (na.Property)
				writer.Write ((byte) 0x54);
			else
				throw new MetadataFormatException ("Unknown kind of namedarg");

			if (na.FixedArg.SzArray)
				writer.Write ((byte) ElementType.SzArray);

			if (na.FieldOrPropType == ElementType.Object)
				writer.Write ((byte) ElementType.Boxed);
			else
				writer.Write ((byte) na.FieldOrPropType);

			if (na.FieldOrPropType == ElementType.Enum)
				Write (na.FixedArg.Elems [0].ElemType.FullName);

			Write (na.FieldOrPropName);

			Write (na.FixedArg, writer);
		}
Beispiel #11
0
		void Write (CustomAttrib ca, MethodReference ctor, MemoryBinaryWriter writer)
		{
			if (ca == null)
				return;

			if (ca.Prolog != CustomAttrib.StdProlog)
				return;

			writer.Write (ca.Prolog);

			for (int i = 0; i < ctor.Parameters.Count; i++)
				Write (ca.FixedArgs [i], writer);

			writer.Write (ca.NumNamed);

			for (int i = 0; i < ca.NumNamed; i++)
				Write (ca.NamedArgs [i], writer);
		}
 void CompressCustomAttribute(CustomAttrib ca, MethodReference ctor, MemoryBinaryWriter writer)
 {
     m_sigWriter.Empty();
     Write(ca, ctor, writer);
 }
 public uint AddCustomAttribute(CustomAttrib ca, MethodReference ctor)
 {
     CompressCustomAttribute(ca, ctor, m_sigWriter);
     return(GetPointer());
 }
Beispiel #14
0
 bool ReadSimpleValue(BinaryReader br, ref CustomAttrib.Elem elem, TypeReference type)
 {
     switch (type.FullName) {
     case Constants.Boolean :
         elem.Value = br.ReadByte () == 1;
         break;
     case Constants.Char :
         elem.Value = (char) br.ReadUInt16 ();
         break;
     case Constants.Single :
         elem.Value = br.ReadSingle ();
         break;
     case Constants.Double :
         elem.Value = br.ReadDouble ();
         break;
     case Constants.Byte :
         elem.Value = br.ReadByte ();
         break;
     case Constants.Int16 :
         elem.Value = br.ReadInt16 ();
         break;
     case Constants.Int32 :
         elem.Value = br.ReadInt32 ();
         break;
     case Constants.Int64 :
         elem.Value = br.ReadInt64 ();
         break;
     case Constants.SByte :
         elem.Value = br.ReadSByte ();
         break;
     case Constants.UInt16 :
         elem.Value = br.ReadUInt16 ();
         break;
     case Constants.UInt32 :
         elem.Value = br.ReadUInt32 ();
         break;
     case Constants.UInt64 :
         elem.Value = br.ReadUInt64 ();
         break;
     default : // enum
         return false;
     }
     elem.Simple = true;
     return true;
 }
Beispiel #15
0
		public byte [] CompressCustomAttribute (CustomAttrib ca, MethodReference ctor)
		{
			MemoryBinaryWriter writer = new MemoryBinaryWriter ();
			CompressCustomAttribute (ca, ctor, writer);
			return writer.ToArray ();
		}
Beispiel #16
0
		void CompressCustomAttribute (CustomAttrib ca, MethodReference ctor, MemoryBinaryWriter writer)
		{
			m_sigWriter.Empty ();
			Write (ca, ctor, writer);
		}
Beispiel #17
0
 TypeReference GetFixedArgType(CustomAttrib.FixedArg fa)
 {
     if (fa.SzArray) {
         if (fa.NumElem == 0)
             return new ArrayType (SearchCoreType (Constants.Object));
         else
             return new ArrayType (fa.Elems [0].ElemType);
     } else
         return fa.Elems [0].ElemType;
 }
Beispiel #18
0
		void Write (CustomAttrib.FixedArg fa, MemoryBinaryWriter writer)
		{
			if (fa.SzArray)
				writer.Write (fa.NumElem);

			foreach (CustomAttrib.Elem elem in fa.Elems)
				Write (elem, writer);
		}
Beispiel #19
0
        TypeReference GetNamedArgType(CustomAttrib.NamedArg na)
        {
            if (na.FieldOrPropType == ElementType.Boxed)
                return SearchCoreType (Constants.Object);

            return GetFixedArgType (na.FixedArg);
        }
Beispiel #20
0
		void Write (CustomAttrib.Elem elem, MemoryBinaryWriter writer) // TODO
		{
			if (elem.String)
				elem.FieldOrPropType = ElementType.String;
			else if (elem.Type)
				elem.FieldOrPropType = ElementType.Type;
			else if (elem.BoxedValueType)
				Write (elem.FieldOrPropType);

			switch (elem.FieldOrPropType) {
			case ElementType.Boolean :
				writer.Write ((byte) ((bool) elem.Value ? 1 : 0));
				break;
			case ElementType.Char :
				writer.Write ((ushort) (char) elem.Value);
				break;
			case ElementType.R4 :
				writer.Write ((float) elem.Value);
				break;
			case ElementType.R8 :
				writer.Write ((double) elem.Value);
				break;
			case ElementType.I1 :
				writer.Write ((sbyte) elem.Value);
				break;
			case ElementType.I2 :
				writer.Write ((short) elem.Value);
				break;
			case ElementType.I4 :
				writer.Write ((int) elem.Value);
				break;
			case ElementType.I8 :
				writer.Write ((long) elem.Value);
				break;
			case ElementType.U1 :
				writer.Write ((byte) elem.Value);
				break;
			case ElementType.U2 :
				writer.Write ((ushort) elem.Value);
				break;
			case ElementType.U4 :
				writer.Write ((uint) elem.Value);
				break;
			case ElementType.U8 :
				writer.Write ((long) elem.Value);
				break;
			case ElementType.String :
			case ElementType.Type :
				string s = elem.Value as string;
				if (s == null)
					writer.Write ((byte) 0xff);
				else if (s.Length == 0)
					writer.Write ((byte) 0x00);
				else
					Write (s);
				break;
			case ElementType.Object :
				if (elem.Value != null)
					throw new NotSupportedException ("Unknown state");
				writer.Write ((byte) 0xff);
				break;
			default :
				throw new NotImplementedException ("WriteElem " + elem.FieldOrPropType.ToString ());
			}
		}
Beispiel #21
0
 CustomAttrib ReadCustomAttrib(int pos, MethodReference ctor, bool resolve)
 {
     int start, length = Utilities.ReadCompressedInteger (m_blobData, pos, out start);
     byte [] data = new byte [length];
     Buffer.BlockCopy (m_blobData, start, data, 0, length);
     try {
         return ReadCustomAttrib (new BinaryReader (
             new MemoryStream (data)), data, ctor, resolve);
     } catch {
         CustomAttrib ca = new CustomAttrib (ctor);
         ca.Read = false;
         return ca;
     }
 }