Beispiel #1
0
 public MethodName AddGenericParameter(string name)
 {
     return(WithGenericParameters(GenericParameters.ImmutableAdd(
                                      GenericParameterName.New(this, GenericParameters.Count, name),
                                      CloneGenericParameter)
                                  ));
 }
Beispiel #2
0
        static TypeName ConvertGenericType(Type type)
        {
            if (type.GetTypeInfo().IsGenericTypeDefinition)
            {
                DefaultTypeName result;
                int             skipCount;
                string          plainName = StripMangle(type.Name);

                if (type.DeclaringType == null)
                {
                    result = new DefaultTypeName(
                        AssemblyName.FromAssemblyName(type.GetTypeInfo().Assembly.GetName()),
                        plainName,
                        type.Namespace);
                    skipCount = 0;
                }
                else
                {
                    var declaring = FromType(type.DeclaringType);
                    result    = new DefaultTypeName(plainName, (DefaultTypeName)declaring);
                    skipCount = declaring.GenericParameterCount;
                }

                var gen = type.GetGenericArguments().Skip(skipCount).Select(
                    (t, i) => GenericParameterName.New(result, i, t.Name)).ToArray();
                result.FinalizeGenerics(gen);

                return(result);
            }

            var args = type.GetGenericArguments().Select(FromType);

            return(FromType(type.GetGenericTypeDefinition()).MakeGenericType(args));
        }
        protected internal virtual string FormatGenericParameter(string format, GenericParameterName name, IFormatProvider formatProvider)
        {
            switch (GetBasicFormat(format))
            {
            case BasicFormat.Compact:
            case BasicFormat.Name:
            case BasicFormat.Full:
                if (GetFormatHelper(formatProvider).UseGenericParameterPositions)
                {
                    return((name.IsMethodGenericParameter ? "``" : "`") + name.Position);
                }
                else
                {
                    return(name.Name);
                }

            case BasicFormat.Roundtrip:
            default:
                StringBuilder buffer = new StringBuilder();

                // System.IComparer`1!T, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
                BuildFullName(buffer, name.DeclaringType, formatProvider);
                buffer.Append('!');
                buffer.Append(name.Name);
                if (name.Assembly != null)
                {
                    buffer.Append(", ");
                    buffer.Append(name.Assembly.FullName);
                }
                return(buffer.ToString());
            }
        }
Beispiel #4
0
        public static TypeName FromType(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (type.IsByRef)
            {
                return(FromType(type.GetElementType()).MakeByReferenceType());
            }

            else if (type.IsArray)
            {
                return(FromType(type.GetElementType()).MakeArrayType(Enumerable.Repeat(ArrayDimension.Unsized, type.GetArrayRank())));
            }

            else if (type.IsPointer)
            {
                return(FromType(type.GetElementType()).MakePointerType());
            }

            else if (type.IsGenericParameter)
            {
                if (type.GetTypeInfo().DeclaringMethod == null)
                {
                    return(GenericParameterName.New(FromType(type.DeclaringType), type.GenericParameterPosition, type.Name));
                }
                else
                {
                    // TODO This is unbound, but getting the method is recursive
                    return(MethodName.GenericParameter(type.GenericParameterPosition));
                }
            }
            else if (type.GetTypeInfo().IsGenericType)
            {
                return(ConvertGenericType(type));
            }

            else
            {
                TypeName declaring = null;
                if (type.DeclaringType == null)
                {
                    return(new DefaultTypeName(
                               AssemblyName.FromAssemblyName(type.GetTypeInfo().Assembly.GetName()),
                               type.Name,
                               type.Namespace ?? string.Empty));
                }
                else
                {
                    declaring = FromType(type.DeclaringType);
                    return(new DefaultTypeName(type.Name, (DefaultTypeName)declaring));
                }
            }
        }
Beispiel #5
0
        private void CopyGenericsTo(DefaultTypeName result)
        {
            if (_genericParameters != null)
            {
                int baseCount = DeclaringType == null ? 0 : DeclaringType.GenericParameterCount;
                var generics  = _genericParameters
                                .Where(t => !(t is RedirectedGenericParameterName))
                                .Select((t, i) => GenericParameterName.New(result, baseCount + i, t.Name)).ToArray();

                result.FinalizeGenerics(generics);
            }
        }
Beispiel #6
0
        internal void FinalizeGenerics(int count)
        {
            GenericParameterName[] names = new GenericParameterName[count];
            int m = DeclaringType == null ? 0 : DeclaringType.GenericParameterCount;

            for (int i = 0; i < count; i++, m++)
            {
                names[i] = GenericParameterName.New(this, m, "`" + m);
            }

            SetGenericParameters(names);
        }
Beispiel #7
0
        public TypeName WithGenericParameters(int count)
        {
            if (count < 0)
            {
                throw Failure.Negative(nameof(count), count);
            }
            GenericParameterName[] names = new GenericParameterName[count];
            int m = 1;

            for (int i = 0; i < count; i++, m++)
            {
                names[i] = GenericParameterName.New(this, m, "`" + m);
            }
            return(SetGenericParameters(names));
        }
Beispiel #8
0
        internal void FinalizeGenerics(int count)
        {
            GenericParameterName[] names = new GenericParameterName[count];
            if (count == 1)
            {
                names[0] = GenericParameterName.New(this, 0, "``0");
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    names[i] = GenericParameterName.New(this, i, "``" + i);
                }
            }

            _genericParameters = new GenericParameterNameCollection(names);
        }
 internal RedirectedGenericParameterName(
     TypeName type, int position, GenericParameterName parent) : base(type)
 {
     _position = position;
     _parent   = parent;
 }
Beispiel #10
0
 private GenericParameterName CloneGenericParameter(GenericParameterName t, int index)
 {
     return(GenericParameterName.New(this, index, t.Name));
 }
Beispiel #11
0
 public MethodName InsertGenericParameterAt(int index, string name)
 {
     return(WithGenericParameters(GenericParameters.ImmutableInsertAt(
                                      index, GenericParameterName.New(this, index, name), CloneGenericParameter
                                      )));
 }
Beispiel #12
0
 public MethodName RemoveGenericParameter(GenericParameterName parameter)
 {
     return(WithGenericParameters(GenericParameters.ImmutableRemove(parameter, CloneGenericParameter)));
 }
Beispiel #13
0
 internal static Arg SetGenericParameters(IEnumerable <string> names)
 {
     return(result => result.FinalizeGenerics(
                names.Select((t, i) => GenericParameterName.New(result, i, t)).ToArray()
                ));
 }
Beispiel #14
0
 internal static Arg SetGenericArguments(Type[] type)
 {
     return(result => result.FinalizeGenerics(
                type.Select((t, i) => GenericParameterName.New(result, i, t.Name)).ToArray()
                ));
 }