Example #1
0
 private static string TypeName(Type type)
 {
     if (type.IsGenericType && type.GetGenericTypeDefinition().FullName.StartsWith("System.Nullable"))
     {
         return(provider.GetTypeOutput(new System.CodeDom.CodeTypeReference(type.GetGenericArguments()[0])) + "?");
     }
     return(provider.GetTypeOutput(new System.CodeDom.CodeTypeReference(type)));
 }
Example #2
0
        /// <summary>
        /// Gets the CSharp type indicated by the specified value.
        /// </summary>
        /// <param name="typeName">Name of the type.</param>
        /// <param name="isNullableType">if set to <c>true</c> [is nullable type].</param>
        /// <returns></returns>
        public static string GetCSharpTypeOutput(string typeName, bool isNullableType)
        {
            Guard.ArgumentNotNullOrEmptyString(typeName, "typeName");

            if (isNullableType &&
                CanBeNullable(typeName))
            {
                typeName = string.Format(CultureInfo.InvariantCulture, "System.Nullable`1[{0}]", typeName);
            }

            string output = csProvider.GetTypeOutput(new CodeTypeReference(typeName));

            return(FormatAlreadyConverted(typeName, output));
        }
        /// <summary>
        /// Detemines whether the specific type name represents a given .NET type within the context of a given
        /// language (only C# and VB are supported). For example, List(Of System.Int32) and List(Of Integer)
        /// within VB both represent the .NET type List``1[System.Int32]
        /// </summary>
        private static bool IsType(string languageSpecificLongTypeName, Type type, string languageGuid)
        {
            // Normalise both type names into language-specific ones with short type names (e.g, System.Collections.Generic.List<int>)
            // since that's the only direction we can go without manually parsing language-specific type names into CLR-style ones
            CodeDomProvider codeDomProvider = string.Equals(languageGuid, VsConstants.VbCodeModelLanguageGuid, StringComparison.OrdinalIgnoreCase)
                                                  ? (CodeDomProvider) new VBCodeProvider()
                                                  : new CSharpCodeProvider();
            var languageSpecificType = codeDomProvider.GetTypeOutput(new CodeTypeReference(type));

            var codeTypeRefLanguageSpecificType = ShortenLanguageSpecificTypeName(languageSpecificLongTypeName, languageGuid);

            return(string.Equals(codeTypeRefLanguageSpecificType, languageSpecificType, StringComparison.Ordinal));
        }
        private ViewLocationResult CreateViewLocationWithModel(Type expectedType, CodeDomProvider provider, string modelDirective)
        {
            var codeTypeRef = new CodeTypeReference(expectedType);
            var modelType   = provider.GetTypeOutput(codeTypeRef);

            var modelTypeCode = BuildCodeExtractingModelType(provider);

            var view = string.Format("@{0} {1}\n\n@{2}", modelDirective, modelType, modelTypeCode);

            return(new ViewLocationResult(
                       string.Empty,
                       string.Empty,
                       string.Concat(provider.FileExtension, "html"),
                       () => new StringReader(view)
                       ));
        }
        private static string [] GetStaticEventDefinitionPath(CodeDomProvider codeDomProvider, CodeMemberEvent @event)
        {
            var isStatic = @event.Attributes.HasBitMask(MemberAttributes.Static) &&
                           @event.PrivateImplementationType == null;

            if (!isStatic)
            {
                return(null);
            }

            var path = new List <string> ( );

            switch (@event.Attributes & MemberAttributes.AccessMask)
            {
            case MemberAttributes.Assembly:
                path.Add("internal ");
                break;

            case MemberAttributes.FamilyAndAssembly:
                path.Add("internal ");
                break;

            case MemberAttributes.Family:
                path.Add("protected ");
                break;

            case MemberAttributes.FamilyOrAssembly:
                path.Add("protected internal ");
                break;

            case MemberAttributes.Private:
                path.Add("private ");
                break;

            case MemberAttributes.Public:
                path.Add("public ");
                break;
            }

            path.Add("event ");
            path.Add(codeDomProvider.GetTypeOutput(@event.Type));
            path.Add(" ");
            path.Add(codeDomProvider.CreateEscapedIdentifier(@event.Name));

            return(path.ToArray( ));
        }
Example #6
0
 public static string GetFriendlyName(Type type)
 {
     return(_codeDomProvider.GetTypeOutput(ResolveTypeReference(type)));
 }
    static string ToCSharp(Type type)
    {
        string r = cs.GetTypeOutput(new CodeTypeReference(type));

        return(r != type.FullName ? r : type.Name);
    }
Example #8
0
        public static string GetCSharpName(this Type type)
        {
            var reference = new System.CodeDom.CodeTypeReference(type);

            return(s_csharpCodeDomProvider.GetTypeOutput(reference));
        }
        internal static string GetTypeName(CodeDomProvider codeProvider, string string1, string string2)
        {
            string str2 = codeProvider.GetTypeOutput(Type(typeof(Activator))).Replace("System", "").Replace("Activator", "");

            return(string1 + str2 + string2);
        }
Example #10
0
        /// <summary>
        /// Gets a compilable, readability-optimized string representation of the specified type.
        /// </summary>
        /// <remarks>
        /// Adapted from: <a href="https://stackoverflow.com/a/6402967/356790" />.
        /// Adapted from: <a href="https://stackoverflow.com/questions/1362884/is-there-a-way-to-get-a-types-alias-through-reflection" />.
        /// </remarks>
        /// <param name="type">The type.</param>
        /// <param name="throwIfNoCompilableStringExists">Optional value indicating whether to throw a <see cref="NotSupportedException"/> if there's no compilable representation of the specified type.</param>
        /// <returns>
        /// A compilable, readability-optimized string representation of the specified type
        /// OR
        /// null if there is no compilable representation and <paramref name="throwIfNoCompilableStringExists"/> is true.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="type"/> is null.</exception>
        /// <exception cref="NotSupportedException"><paramref name="throwIfNoCompilableStringExists"/> is true and <paramref name="type"/> is a generic open constructed type, which is not supported.</exception>
        /// <exception cref="NotSupportedException"><paramref name="throwIfNoCompilableStringExists"/> is true and <paramref name="type"/> is a generic parameter.</exception>
        public static string ToStringCompilable(
            this Type type,
            bool throwIfNoCompilableStringExists = false)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            string result;

            if (type.IsClosedAnonymousType())
            {
                if (throwIfNoCompilableStringExists)
                {
                    throw new NotSupportedException("Anonymous types are not supported.");
                }
                else
                {
                    result = null;
                }
            }
            else if (type.IsGenericParameter)
            {
                if (throwIfNoCompilableStringExists)
                {
                    // note that IsGenericParameter and ContainsGenericParameters will return true for generic parameters,
                    // hence we order the IsGenericParameter check first.
                    throw new NotSupportedException("Generic parameters not supported.");
                }
                else
                {
                    result = null;
                }
            }
            else
            {
                if (ValueTypeToAliasMap.ContainsKey(type))
                {
                    result = ValueTypeToAliasMap[type];
                }
                else if (type.IsClosedNullableType())
                {
                    result = Nullable.GetUnderlyingType(type).ToStringCompilable() + "?";
                }
                else if (type.IsArray)
                {
                    result = type.GetElementType().ToStringCompilable() + "[]";
                }
                else
                {
                    result = CodeDomProvider.GetTypeOutput(new CodeTypeReference(type.FullName?.Replace(type.Namespace + ".", string.Empty)));

                    if (type.IsGenericType)
                    {
                        if (type.IsGenericTypeDefinition)
                        {
                            result = result.Replace(" ", string.Empty);
                        }
                        else if (type.ContainsGenericParameters)
                        {
                            if (throwIfNoCompilableStringExists)
                            {
                                throw new NotSupportedException("Generic open constructed types are not supported.");
                            }
                            else
                            {
                                result = null;
                            }
                        }
                        else
                        {
                            var genericParameters = type.GenericTypeArguments.Select(_ => _.ToStringCompilable()).ToArray();

                            result = GenericBracketsRegex.Replace(result, "<" + string.Join(", ", genericParameters) + ">");
                        }
                    }
                }
            }

            return(result);
        }
Example #11
0
 public string GetTypeOutput(CodeTypeReference type)
 {
     return(provider.GetTypeOutput(type));
 }