public static string[] GetGenericName(Mono.Collections.Generic.Collection <TypeReference> types, int offset, int count, bool bFull)
    {
        string[] results = new string[count];

        for (int i = 0; i < count; i++)
        {
            int pos = i + offset;

            if (types[pos].IsGenericInstance)
            {
                results[i] = GetGenericName(types[pos], bFull);
            }
            else
            {
                results[i] = GetTypeName(types[pos]);
            }
        }

        return(results);
    }
Example #2
0
 static public int Emit(this MethodBody body, OpCode instruction, TypeReference type, Mono.Collections.Generic.Collection <ParameterDefinition> parameters)
 {
     if (instruction == OpCodes.Calli)
     {
         var _signature = new CallSite(type);
         foreach (var _parameter in parameters)
         {
             _signature.Parameters.Add(_parameter);
         }
         _signature.CallingConvention = MethodCallingConvention.Default;
         return(body.Add(Mono.Cecil.Cil.Instruction.Create(instruction, _signature)));
     }
     throw new InvalidOperationException();
 }
        private StringBuilder AppendConstraints(StringBuilder buf, IList <GenericParameter> genArgs)
        {
            foreach (GenericParameter genArg in genArgs)
            {
                GenericParameterAttributes attrs = genArg.Attributes;
#if NEW_CECIL
                Mono.Collections.Generic.Collection <GenericParameterConstraint> constraints = genArg.Constraints;
#else
                IList <TypeReference> constraints = genArg.Constraints;
#endif
                if (attrs == GenericParameterAttributes.NonVariant && constraints.Count == 0)
                {
                    continue;
                }

                bool isref = (attrs & GenericParameterAttributes.ReferenceTypeConstraint) != 0;
                bool isvt  = (attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0;
                bool isnew = (attrs & GenericParameterAttributes.DefaultConstructorConstraint) != 0;
                bool comma = false;

                if (!isref && !isvt && !isnew && constraints.Count == 0)
                {
                    continue;
                }
                buf.Append(" where ").Append(genArg.Name).Append(" : ");
                if (isref)
                {
                    buf.Append("class");
                    comma = true;
                }
                else if (isvt)
                {
                    buf.Append("value class");
                    comma = true;
                }
                if (constraints.Count > 0 && !isvt)
                {
                    if (comma)
                    {
                        buf.Append(", ");
                    }

#if NEW_CECIL
                    buf.Append(GetTypeName(constraints[0].ConstraintType));
                    for (int i = 1; i < constraints.Count; ++i)
                    {
                        buf.Append(", ").Append(GetTypeName(constraints[i].ConstraintType));
                    }
#else
                    buf.Append(GetTypeName(constraints[0]));
                    for (int i = 1; i < constraints.Count; ++i)
                    {
                        buf.Append(", ").Append(GetTypeName(constraints[i]));
                    }
#endif
                }
                if (isnew && !isvt)
                {
                    if (comma)
                    {
                        buf.Append(", ");
                    }
                    buf.Append("gcnew()");
                }
            }
            return(buf);
        }
Example #4
0
        private void AppendConstraints(StringBuilder buf, IList <GenericParameter> genArgs)
        {
            var origMemberFormatterState = MemberFormatterState;

            MemberFormatterState = MemberFormatterState.WithinGenericTypeParameters;
            List <string> constraintStrings = new List <string>();

            foreach (GenericParameter genArg in genArgs.Where(i => !IsFlexibleType(i)))
            {
                GenericParameterAttributes attrs = genArg.Attributes;

#if NEW_CECIL
                Mono.Collections.Generic.Collection <GenericParameterConstraint> constraints = genArg.Constraints;
#else
                IList <TypeReference> constraints = genArg.Constraints;
#endif
                if (attrs == GenericParameterAttributes.NonVariant && constraints.Count == 0)
                {
                    continue;
                }

                bool isref = (attrs & GenericParameterAttributes.ReferenceTypeConstraint) != 0;
                bool isvt  = (attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0;
                bool isnew = (attrs & GenericParameterAttributes.DefaultConstructorConstraint) != 0;

                if (!isref && !isvt && !isnew && constraints.Count == 0)
                {
                    continue;
                }

                var genericName = GetGenericName(genArg.Name);
                if (isref)
                {
                    // May be "not struct". We don't know it here
                    constraintStrings.Add($"{genericName} : null");
                }
                else if (isvt)
                {
                    constraintStrings.Add($"{genericName} : struct");
                }
                if (constraints.Count > 0 && !isvt)
                {
                    foreach (var typeReference in constraints)
                    {
#if NEW_CECIL
                        constraintStrings.Add($"{genericName} :> {GetTypeName(typeReference.ConstraintType)}");
#else
                        constraintStrings.Add($"{genericName} :> {GetTypeName(typeReference)}");
#endif
                    }
                }
                if (isnew && !isvt)
                {
                    constraintStrings.Add($"{genericName} : (new : unit -> {GetTypeName(genArg)})");
                }
            }

            if (constraintStrings.Count > 0)
            {
                buf.Append($" (requires {string.Join(" and ", constraintStrings)})");
            }
            MemberFormatterState = origMemberFormatterState;
        }
        public static List <FieldDefinition> GetEnumFieldDefinitionByValue(Mono.Collections.Generic.Collection <FieldDefinition> fieldDefinitions, object value, Collection <Mono.Cecil.CustomAttribute> customAttributes)
        {
            bool flagsEnum = false;

            foreach (CustomAttribute attribute in customAttributes)
            {
                if (attribute.AttributeType.FullName == "System.FlagsAttribute")
                {
                    flagsEnum = true;
                }
            }

            long valueAsInt64;

            if (value is string)
            {
                if (!long.TryParse((string)value, out valueAsInt64))
                {
                    valueAsInt64 = 0;
                }
            }
            else
            {
                valueAsInt64 = Convert.ToInt64(value);
            }
            List <FieldDefinition> result = new List <Mono.Cecil.FieldDefinition>();

            for (int i = 1; i < fieldDefinitions.Count; i++)
            {
                if (fieldDefinitions[i].Constant == null || fieldDefinitions[i].Constant.Value == null)
                {
                    continue;
                }

                //Checking whether value corresponds directly to an enum field
                if (fieldDefinitions[i].Constant.Value.Equals(value))
                {
                    result.Clear();
                    result.Add(fieldDefinitions[i]);
                    return(result);
                }

                if (flagsEnum)
                {
                    ////After this there might be duplicates, i.e. assuming that there are enum.A, enum.B and enum.AB where enum.AB = enum.A | enum.B all the three are gonna be present in the result.
                    ////Which is correct although ugly. TODO: Fix duplication of flags in enums.
                    //Assuming the largest underlying type of an enum is 64-bit signed/unsigned int
                    long enumConstantasInt64 = Convert.ToInt64(fieldDefinitions[i].Constant.Value);
                    if (enumConstantasInt64 != 0)
                    {
                        if ((enumConstantasInt64 & valueAsInt64) == enumConstantasInt64)
                        {
                            result.Add(fieldDefinitions[i]);
                        }
                    }
                    else
                    {
                        if (valueAsInt64 == 0)
                        {
                            result.Add(fieldDefinitions[i]);
                        }
                    }
                }
            }

            return(result);
        }