System.Xml.Serialization.TypeData.ToCSharpName C# (CSharp) Method

ToCSharpName() public static method

public static ToCSharpName ( Type type, bool full ) : string
type System.Type
full bool
return string
		public static string ToCSharpName (Type type, bool full)
		{
			// return csprovider.GetTypeOutput (new System.CodeDom.CodeTypeReference (type));
			StringBuilder sb = new StringBuilder ();
			
			if (type.IsArray) {
				sb.Append (ToCSharpName (type.GetElementType (), full));
				sb.Append ('[');
				int rank = type.GetArrayRank ();
				for (int i = 1; i < rank; i++)
					sb.Append (',');
				sb.Append (']');
				return sb.ToString ();
			}
#if NET_2_0
			// Generic nested types return the complete list of type arguments,
			// including type arguments for the declaring class. This requires
			// some special handling
			if (type.IsGenericType && !type.IsGenericTypeDefinition) {
				Type[] args = type.GetGenericArguments ();
				int nt = args.Length - 1;
				Type pt = type;
				// Loop throguh the declaring class chain, consuming type arguments for every
				// generic class in the chain
				while (pt != null) {
					if (sb.Length > 0)
						sb.Insert (0, '.');
					int i = pt.Name.IndexOf ('`');
					if (i != -1) {
						int na = nt - int.Parse (pt.Name.Substring (i+1));
						sb.Insert (0,'>');
						for (;nt > na; nt--) {
							sb.Insert (0, ToCSharpName (args[nt],full));
							if (nt - 1 != na)
								sb.Insert (0, ',');
						}
						sb.Insert (0,'<');
						sb.Insert (0, pt.Name.Substring (0, i));
					} else
						sb.Insert (0, pt.Name);
					pt = pt.DeclaringType;
				}
				if (full && type.Namespace.Length > 0)
					sb.Insert (0, type.Namespace + ".");
				return sb.ToString ();
			}
#endif
			if (type.DeclaringType != null) {
				sb.Append (ToCSharpName (type.DeclaringType, full)).Append ('.');
				sb.Append (type.Name);
			}
			else {
				if (full && type.Namespace.Length > 0)
					sb.Append (type.Namespace).Append ('.');
				sb.Append (type.Name);
			}
			return sb.ToString ();
		}
		

Usage Example

Example #1
0
        public static string ToCSharpName(Type type, bool full)
        {
            if (type.IsArray)
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append(TypeData.ToCSharpName(type.GetElementType(), full));
                stringBuilder.Append('[');
                int arrayRank = type.GetArrayRank();
                for (int i = 1; i < arrayRank; i++)
                {
                    stringBuilder.Append(',');
                }
                stringBuilder.Append(']');
                return(stringBuilder.ToString());
            }
            if (type.IsGenericType && !type.IsGenericTypeDefinition)
            {
                StringBuilder stringBuilder2 = new StringBuilder();
                stringBuilder2.Append(TypeData.ToCSharpName(type.GetGenericTypeDefinition(), full));
                stringBuilder2.Append('<');
                foreach (Type type2 in type.GetGenericArguments())
                {
                    stringBuilder2.Append(TypeData.ToCSharpName(type2, full)).Append(',');
                }
                stringBuilder2.Length--;
                stringBuilder2.Append('>');
                return(stringBuilder2.ToString());
            }
            string text = (!full) ? type.Name : type.FullName;

            text = text.Replace('+', '.');
            int num = text.IndexOf('`');

            text = ((num <= 0) ? text : text.Substring(0, num));
            if (TypeData.IsKeyword(text))
            {
                return("@" + text);
            }
            return(text);
        }