Пример #1
0
        public void FieldOffsetAttribute()
        {
            IField field = ctx.GetClass(typeof(ExplicitFieldLayoutStruct)).Fields.Single(f => f.Name == "Field0");

            Assert.AreEqual("System.Runtime.InteropServices.FieldOffsetAttribute", field.Attributes.Single().AttributeType.Resolve(ctx).FullName);
            IConstantValue arg = field.Attributes.Single().PositionalArguments.Single();

            Assert.AreEqual("System.Int32", arg.GetValueType(ctx).FullName);
            Assert.AreEqual(0, arg.GetValue(ctx));

            field = ctx.GetClass(typeof(ExplicitFieldLayoutStruct)).Fields.Single(f => f.Name == "Field100");
            Assert.AreEqual("System.Runtime.InteropServices.FieldOffsetAttribute", field.Attributes.Single().AttributeType.Resolve(ctx).FullName);
            arg = field.Attributes.Single().PositionalArguments.Single();
            Assert.AreEqual("System.Int32", arg.GetValueType(ctx).FullName);
            Assert.AreEqual(100, arg.GetValue(ctx));
        }
Пример #2
0
 public object GetDefaultValue(ITypeResolveContext context)
 {
     if (defaultValue == null)
     {
         throw new InvalidOperationException();
     }
     else
     {
         return(defaultValue.GetValue(context));
     }
 }
Пример #3
0
        public object GetValue(ITypeResolveContext context)
        {
            object val = baseValue.GetValue(context);

            if (val == null)
            {
                return(null);
            }
            TypeCode typeCode = Type.GetTypeCode(val.GetType());

            if (!(typeCode >= TypeCode.SByte && typeCode <= TypeCode.UInt64))
            {
                return(null);
            }
            long intVal = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, val, false);

            return(CSharpPrimitiveCast.Cast(typeCode, unchecked (intVal + incrementAmount), false));
        }
Пример #4
0
        public void ExplicitStructLayoutAttribute()
        {
            IAttribute attr = ctx.GetClass(typeof(ExplicitFieldLayoutStruct)).Attributes.Single();

            Assert.AreEqual("System.Runtime.InteropServices.StructLayoutAttribute", attr.AttributeType.Resolve(ctx).FullName);
            IConstantValue arg1 = attr.PositionalArguments.Single();

            Assert.AreEqual("System.Runtime.InteropServices.LayoutKind", arg1.GetValueType(ctx).FullName);
            Assert.AreEqual((int)LayoutKind.Explicit, arg1.GetValue(ctx));

            var arg2 = attr.NamedArguments[0];

            Assert.AreEqual("CharSet", arg2.Key);
            Assert.AreEqual("System.Runtime.InteropServices.CharSet", arg2.Value.GetValueType(ctx).FullName);
            Assert.AreEqual((int)CharSet.Unicode, arg2.Value.GetValue(ctx));

            var arg3 = attr.NamedArguments[1];

            Assert.AreEqual("Pack", arg3.Key);
            Assert.AreEqual("System.Int32", arg3.Value.GetValueType(ctx).FullName);
            Assert.AreEqual(8, arg3.Value.GetValue(ctx));
        }
Пример #5
0
        public IMethod ResolveConstructor(ITypeResolveContext context)
        {
            CSharpResolver r    = new CSharpResolver(context);
            IType          type = attributeType.Resolve(context);
            int            totalArgumentCount = 0;

            if (positionalArguments != null)
            {
                totalArgumentCount += positionalArguments.Count;
            }
            if (namedCtorArguments != null)
            {
                totalArgumentCount += namedCtorArguments.Count;
            }
            ResolveResult[] arguments     = new ResolveResult[totalArgumentCount];
            string[]        argumentNames = new string[totalArgumentCount];
            int             i             = 0;

            if (positionalArguments != null)
            {
                while (i < positionalArguments.Count)
                {
                    IConstantValue cv = positionalArguments[i];
                    arguments[i] = new ConstantResolveResult(cv.GetValueType(context), cv.GetValue(context));
                    i++;
                }
            }
            if (namedCtorArguments != null)
            {
                foreach (var pair in namedCtorArguments)
                {
                    argumentNames[i] = pair.Key;
                    arguments[i]     = new ConstantResolveResult(pair.Value.GetValueType(context), pair.Value.GetValue(context));
                    i++;
                }
            }
            MemberResolveResult mrr = r.ResolveObjectCreation(type, arguments, argumentNames) as MemberResolveResult;

            return(mrr != null ? mrr.Member as IMethod : null);
        }