// // Computes the ToString() value for a CustomAttributeTypedArgument struct. // private static String ComputeTypedArgumentString(CustomAttributeTypedArgument cat, bool typed) { Type argumentType = cat.ArgumentType; if (argumentType == null) { return(cat.ToString()); } FoundationTypes foundationTypes = argumentType.AsConfirmedRuntimeType().GetReflectionDomain().FoundationTypes; Object value = cat.Value; TypeInfo argumentTypeInfo = argumentType.GetTypeInfo(); if (argumentTypeInfo.IsEnum) { return(String.Format(typed ? "{0}" : "({1}){0}", value, argumentType.FullName)); } if (value == null) { return(String.Format(typed ? "null" : "({0})null", argumentType.Name)); } if (argumentType.Equals(foundationTypes.SystemString)) { return(String.Format("\"{0}\"", value)); } if (argumentType.Equals(foundationTypes.SystemChar)) { return(String.Format("'{0}'", value)); } if (argumentType.Equals(foundationTypes.SystemType)) { return(String.Format("typeof({0})", ((Type)value).FullName)); } else if (argumentType.IsArray) { String result = null; IList <CustomAttributeTypedArgument> array = value as IList <CustomAttributeTypedArgument>; Type elementType = argumentType.GetElementType(); result = String.Format(@"new {0}[{1}] {{ ", elementType.GetTypeInfo().IsEnum ? elementType.FullName : elementType.Name, array.Count); for (int i = 0; i < array.Count; i++) { result += String.Format(i == 0 ? "{0}" : ", {0}", ComputeTypedArgumentString(array[i], elementType != foundationTypes.SystemObject)); } return(result += " }"); } return(String.Format(typed ? "{0}" : "({1}){0}", value, argumentType.Name)); }
// // Computes the ToString() value for a CustomAttributeTypedArgument struct. // private static String ComputeTypedArgumentString(CustomAttributeTypedArgument cat, bool typed) { Type argumentType = cat.ArgumentType; if (argumentType == null) { return(cat.ToString()); } Object value = cat.Value; if (argumentType.IsEnum) { return(String.Format(typed ? "{0}" : "({1}){0}", value, argumentType.FullName)); } if (value == null) { return(String.Format(typed ? "null" : "({0})null", argumentType.Name)); } if (argumentType.Equals(CommonRuntimeTypes.String)) { return(String.Format("\"{0}\"", value)); } if (argumentType.Equals(CommonRuntimeTypes.Char)) { return(String.Format("'{0}'", value)); } if (argumentType.Equals(CommonRuntimeTypes.Type)) { return(String.Format("typeof({0})", ((Type)value).FullName)); } else if (argumentType.IsArray) { String result = null; IList <CustomAttributeTypedArgument> array = value as IList <CustomAttributeTypedArgument>; Type elementType = argumentType.GetElementType(); result = String.Format(@"new {0}[{1}] {{ ", elementType.IsEnum ? elementType.FullName : elementType.Name, array.Count); for (int i = 0; i < array.Count; i++) { result += String.Format(i == 0 ? "{0}" : ", {0}", ComputeTypedArgumentString(array[i], elementType != CommonRuntimeTypes.Object)); } return(result += " }"); } return(String.Format(typed ? "{0}" : "({1}){0}", value, argumentType.Name)); }
// // Computes the ToString() value for a CustomAttributeTypedArgument struct. // private static string ComputeTypedArgumentString(CustomAttributeTypedArgument cat, bool typed) { Type argumentType = cat.ArgumentType; if (argumentType == null) { return(cat.ToString()); } object?value = cat.Value; if (argumentType.IsEnum) { return(string.Format(typed ? "{0}" : "({1}){0}", value, argumentType.FullName)); } if (value == null) { return(string.Format(typed ? "null" : "({0})null", argumentType.Name)); } if (argumentType == typeof(string)) { return(string.Format("\"{0}\"", value)); } if (argumentType == typeof(char)) { return(string.Format("'{0}'", value)); } if (argumentType == typeof(Type)) { return(string.Format("typeof({0})", ((Type)value).FullName)); } else if (argumentType.IsArray) { IList <CustomAttributeTypedArgument> array = (IList <CustomAttributeTypedArgument>)value; Type elementType = argumentType.GetElementType() !; string result = string.Format(@"new {0}[{1}] {{ ", elementType.IsEnum ? elementType.FullName : elementType.Name, array.Count); for (int i = 0; i < array.Count; i++) { result += string.Format(i == 0 ? "{0}" : ", {0}", ComputeTypedArgumentString(array[i], elementType != typeof(object))); } return(result += " }"); } return(string.Format(typed ? "{0}" : "({1}){0}", value, argumentType.Name)); }
private static string NormalizeCustomAttributeTypedArgument(CustomAttributeTypedArgument arg) { if (arg.ArgumentType == null) { return(arg.ToString()); } var normalizedTypeName = arg.ArgumentType.GetFullName(); if (arg.ArgumentType.IsEnum) { // casting the value to int in the case of an enum // solves the same issue as the same workaround solves in // BuildDefaultParameterValueToken, below, on linux/mac return($"({normalizedTypeName}){(int)arg.Value}"); } if (arg.Value == null) { return($"({normalizedTypeName})null"); } if (arg.ArgumentType == typeof(string)) { return($"\"{arg.Value}\""); } if (arg.ArgumentType == typeof(char)) { return($"'{arg.Value}'"); } if (arg.ArgumentType == typeof(Type)) { return($"typeof({normalizedTypeName})"); } if (!arg.ArgumentType.IsArray) { return($"({normalizedTypeName}){arg.Value}"); } // if it's an array, compose those tokens. var argList = arg.Value as IList <CustomAttributeTypedArgument>; var arrayType = arg.ArgumentType.GetElementType().GetFullName(); return($"new {arrayType}[{argList.Count}] {{ {string.Join(", ", argList.Select(NormalizeCustomAttributeTypedArgument))} }}"); }
// // Computes the ToString() value for a CustomAttributeTypedArgument struct. // private static String ComputeTypedArgumentString(CustomAttributeTypedArgument cat, bool typed) { Type argumentType = cat.ArgumentType; if (argumentType == null) return cat.ToString(); FoundationTypes foundationTypes = argumentType.AsConfirmedRuntimeType().GetReflectionDomain().FoundationTypes; Object value = cat.Value; TypeInfo argumentTypeInfo = argumentType.GetTypeInfo(); if (argumentTypeInfo.IsEnum) return String.Format(typed ? "{0}" : "({1}){0}", value, argumentType.FullName); if (value == null) return String.Format(typed ? "null" : "({0})null", argumentType.Name); if (argumentType.Equals(foundationTypes.SystemString)) return String.Format("\"{0}\"", value); if (argumentType.Equals(foundationTypes.SystemChar)) return String.Format("'{0}'", value); if (argumentType.Equals(foundationTypes.SystemType)) return String.Format("typeof({0})", ((Type)value).FullName); else if (argumentType.IsArray) { String result = null; IList<CustomAttributeTypedArgument> array = value as IList<CustomAttributeTypedArgument>; Type elementType = argumentType.GetElementType(); result = String.Format(@"new {0}[{1}] {{ ", elementType.GetTypeInfo().IsEnum ? elementType.FullName : elementType.Name, array.Count); for (int i = 0; i < array.Count; i++) result += String.Format(i == 0 ? "{0}" : ", {0}", ComputeTypedArgumentString(array[i], elementType != foundationTypes.SystemObject)); return result += " }"; } return String.Format(typed ? "{0}" : "({1}){0}", value, argumentType.Name); }
public static void Test_CustomAttributeTypedArgument_ToString() { var argument = new CustomAttributeTypedArgument(new [] { new CustomAttributeTypedArgument(0) }); Assert.Equal("new CustomAttributeTypedArgument[1] { 0 }", argument.ToString()); }