public string GetSignature(FieldInfo field) { if (field == null) { throw new ArgumentNullException("field"); } var typeReferencer = new CSharpTypeReferenceProvider(field.ReflectedType); var sb = new StringBuilder(); if (field.IsLiteral) { sb.Append("const "); } else if (field.IsStatic) { sb.Append("static "); } if (field.IsInitOnly) { sb.Append("readonly "); } sb.Append(typeReferencer.GetTypeReference(field.FieldType)); sb.Append(" "); sb.Append(field.Name); return(sb.ToString()); }
public string GetSignature(Type type) { if (type == null) { throw new ArgumentNullException("type"); } var typeReferencer = new CSharpTypeReferenceProvider(type); var sb = new StringBuilder(); if (type.IsClass) { if (IsDelegateType(type)) { AppendMetaType(type, sb); var method = type.GetMethod("Invoke"); sb.Append(typeReferencer.GetTypeReference(method.ReturnType)); sb.Append(" "); AppendTypeName(type, sb); AppendMethodParameters(method, sb, typeReferencer); } else { AppendNonAccessModifiers(type, sb); AppendMetaType(type, sb); AppendTypeName(type, sb); AppendBaseTypeAndInterfaces(type, sb, typeReferencer); } } else if (type.IsInterface) { AppendMetaType(type, sb); AppendTypeName(type, sb); AppendBaseTypeAndInterfaces(type, sb, typeReferencer); } else { // not a class & not an interface => value type if (type.IsEnum) { AppendMetaType(type, sb); AppendTypeName(type, sb); } else { // not an enum => struct AppendMetaType(type, sb); AppendTypeName(type, sb); AppendBaseTypeAndInterfaces(type, sb, typeReferencer); } } return(sb.ToString()); }
public string GetSignature(PropertyInfo property) { if (property == null) { throw new ArgumentNullException("property"); } var typeReferencer = new CSharpTypeReferenceProvider(property.ReflectedType); var sb = new StringBuilder(); var getter = property.GetGetMethod(false); var setter = property.GetSetMethod(false); if (!property.ReflectedType.IsInterface) { var accessor = getter ?? setter; if (accessor != null) { AppendNonAccessModifiers(accessor, sb); } } sb.Append(typeReferencer.GetTypeReference(property.PropertyType)); sb.Append(" "); var indexers = property.GetIndexParameters(); if (indexers.Length > 0) { sb.Append("this["); sb.Append(FormatParameters(indexers, typeReferencer)); sb.Append("]"); } else { sb.Append(property.Name); } sb.Append(" { "); if (getter != null) { sb.Append("get; "); } if (setter != null) { sb.Append("set; "); } sb.Append("}"); return(sb.ToString()); }
public string GetSignature(MethodInfo method) { if (method == null) { throw new ArgumentNullException("method"); } var typeReferencer = new CSharpTypeReferenceProvider(method.ReflectedType); var sb = new StringBuilder(); if (method.Name == "Finalize") { sb.Append("~"); sb.Append(CSharpTypeReferenceProvider.GetBaseName(method.ReflectedType)); sb.Append("()"); return(sb.ToString()); } if (!method.ReflectedType.IsInterface) { AppendNonAccessModifiers(method, sb); } var operatorNameBeforeType = false; if (method.IsSpecialName && (method.Name.Equals("op_Explicit") || method.Name.Equals("op_Implicit"))) { sb.Append(Operators[method.Name]); sb.Append(" "); operatorNameBeforeType = true; } sb.Append(typeReferencer.GetTypeReference(method.ReturnType)); if (!operatorNameBeforeType) { sb.Append(" "); if (method.IsSpecialName && Operators.ContainsKey(method.Name)) { sb.Append(Operators[method.Name]); } else { sb.Append(method.Name); } } AppendGenericDeclaration(method, sb, typeReferencer); AppendMethodParameters(method, sb, typeReferencer); return(sb.ToString()); }
public string GetSignature(ConstructorInfo constructor) { if (constructor == null) { throw new ArgumentNullException("constructor"); } var typeReferencer = new CSharpTypeReferenceProvider(constructor.ReflectedType); var sb = new StringBuilder(); AppendNonAccessModifiers(constructor, sb); sb.Append(CSharpTypeReferenceProvider.GetBaseName(constructor.ReflectedType)); AppendMethodParameters(constructor, sb, typeReferencer); return(sb.ToString()); }
private static string FormatParameter(ParameterInfo p, CSharpTypeReferenceProvider typeReferencer) { if (p.ParameterType.IsByRef) { var elementType = typeReferencer.GetTypeReference(p.ParameterType.GetElementType()); if (p.IsOut) { return("out " + elementType); } else { return("ref " + elementType); } } else { return(typeReferencer.GetTypeReference(p.ParameterType)); } }
public void ShouldReplaceSystemTypesWithTheirCSharpBuiltInTypes() { // Built-In Types Table (C# Reference): // http://msdn.microsoft.com/en-us/library/ya5y69ds.aspx var typeReferencer = new CSharpTypeReferenceProvider(); Assert.That(typeReferencer.GetTypeReference(typeof(System.Boolean)), Is.EqualTo("bool")); Assert.That(typeReferencer.GetTypeReference(typeof(System.Byte)), Is.EqualTo("byte")); Assert.That(typeReferencer.GetTypeReference(typeof(System.SByte)), Is.EqualTo("sbyte")); Assert.That(typeReferencer.GetTypeReference(typeof(System.Char)), Is.EqualTo("char")); Assert.That(typeReferencer.GetTypeReference(typeof(System.Decimal)), Is.EqualTo("decimal")); Assert.That(typeReferencer.GetTypeReference(typeof(System.Double)), Is.EqualTo("double")); Assert.That(typeReferencer.GetTypeReference(typeof(System.Single)), Is.EqualTo("float")); Assert.That(typeReferencer.GetTypeReference(typeof(System.Int32)), Is.EqualTo("int")); Assert.That(typeReferencer.GetTypeReference(typeof(System.UInt32)), Is.EqualTo("uint")); Assert.That(typeReferencer.GetTypeReference(typeof(System.Int64)), Is.EqualTo("long")); Assert.That(typeReferencer.GetTypeReference(typeof(System.UInt64)), Is.EqualTo("ulong")); Assert.That(typeReferencer.GetTypeReference(typeof(System.Object)), Is.EqualTo("object")); Assert.That(typeReferencer.GetTypeReference(typeof(System.Int16)), Is.EqualTo("short")); Assert.That(typeReferencer.GetTypeReference(typeof(System.UInt16)), Is.EqualTo("ushort")); Assert.That(typeReferencer.GetTypeReference(typeof(System.String)), Is.EqualTo("string")); }
private static void AppendBaseTypeAndInterfaces(Type type, StringBuilder sb, CSharpTypeReferenceProvider typeReferencer) { var classBase = new List <string>(); if (type.BaseType != null && type.BaseType != typeof(object) && type.BaseType != typeof(ValueType)) { classBase.Add(typeReferencer.GetTypeReference(type.BaseType)); } foreach (var interfaceDeclaration in type.GetInterfaces()) { classBase.Add(typeReferencer.GetTypeReference(interfaceDeclaration)); } if (classBase.Count > 0) { sb.Append(" : "); sb.Append(string.Join(", ", classBase)); } }
public string GetSignature(EventInfo event1) { if (event1 == null) { throw new ArgumentNullException("event1"); } var typeReferencer = new CSharpTypeReferenceProvider(event1.ReflectedType); var sb = new StringBuilder(); if (!event1.ReflectedType.IsInterface) { var adder = event1.GetAddMethod(true); AppendNonAccessModifiers(adder, sb); } sb.Append("event "); sb.Append(typeReferencer.GetTypeReference(event1.EventHandlerType)); sb.Append(" "); sb.Append(event1.Name); return(sb.ToString()); }
public string GetSignature(Type type) { if (type == null) throw new ArgumentNullException("type"); var typeReferencer = new CSharpTypeReferenceProvider(type); var sb = new StringBuilder(); if (type.IsClass) { if (IsDelegateType(type)) { AppendMetaType(type, sb); var method = type.GetMethod("Invoke"); sb.Append(typeReferencer.GetTypeReference(method.ReturnType)); sb.Append(" "); AppendTypeName(type, sb); AppendMethodParameters(method, sb, typeReferencer); } else { AppendNonAccessModifiers(type, sb); AppendMetaType(type, sb); AppendTypeName(type, sb); AppendBaseTypeAndInterfaces(type, sb, typeReferencer); } } else if (type.IsInterface) { AppendMetaType(type, sb); AppendTypeName(type, sb); AppendBaseTypeAndInterfaces(type, sb, typeReferencer); } else { // not a class & not an interface => value type if (type.IsEnum) { AppendMetaType(type, sb); AppendTypeName(type, sb); } else { // not an enum => struct AppendMetaType(type, sb); AppendTypeName(type, sb); AppendBaseTypeAndInterfaces(type, sb, typeReferencer); } } return sb.ToString(); }
private static string FormatParameters(IEnumerable<ParameterInfo> parameters, CSharpTypeReferenceProvider typeReferencer) { var paramStrings = parameters .Select(p => string.Format( CultureInfo.InvariantCulture, "{0} {1}", FormatParameter(p, typeReferencer), p.Name)) .ToArray(); return string.Join(", ", paramStrings); }
private static string FormatParameter(ParameterInfo p, CSharpTypeReferenceProvider typeReferencer) { if (p.ParameterType.IsByRef) { var elementType = typeReferencer.GetTypeReference(p.ParameterType.GetElementType()); if (p.IsOut) { return "out " + elementType; } else { return "ref " + elementType; } } else { return typeReferencer.GetTypeReference(p.ParameterType); } }
private static void AppendMethodParameters(MethodBase method, StringBuilder sb, CSharpTypeReferenceProvider typeReferencer) { sb.Append("("); if (IsExtensionMethod(method)) { sb.Append("this "); } sb.Append(FormatParameters(method.GetParameters(), typeReferencer)); sb.Append(")"); }
private static void AppendGenericDeclaration(MethodInfo method, StringBuilder sb, CSharpTypeReferenceProvider typeReferencer) { if (method.IsGenericMethod) { sb.Append('<'); sb.Append(string.Join(", ", method.GetGenericArguments().Select(x => typeReferencer.GetTypeReference(x)))); sb.Append('>'); } }
private static void AppendBaseTypeAndInterfaces(Type type, StringBuilder sb, CSharpTypeReferenceProvider typeReferencer) { var classBase = new List<string>(); if (type.BaseType != null && type.BaseType != typeof(object) && type.BaseType != typeof(ValueType)) { classBase.Add(typeReferencer.GetTypeReference(type.BaseType)); } foreach (var interfaceDeclaration in type.GetInterfaces()) { classBase.Add(typeReferencer.GetTypeReference(interfaceDeclaration)); } if (classBase.Count > 0) { sb.Append(" : "); sb.Append(string.Join(", ", classBase)); } }
public string GetSignature(EventInfo event1) { if (event1 == null) throw new ArgumentNullException("event1"); var typeReferencer = new CSharpTypeReferenceProvider(event1.ReflectedType); var sb = new StringBuilder(); if (!event1.ReflectedType.IsInterface) { var adder = event1.GetAddMethod(true); AppendNonAccessModifiers(adder, sb); } sb.Append("event "); sb.Append(typeReferencer.GetTypeReference(event1.EventHandlerType)); sb.Append(" "); sb.Append(event1.Name); return sb.ToString(); }
public string GetSignature(FieldInfo field) { if (field == null) throw new ArgumentNullException("field"); var typeReferencer = new CSharpTypeReferenceProvider(field.ReflectedType); var sb = new StringBuilder(); if (field.IsLiteral) { sb.Append("const "); } else if (field.IsStatic) { sb.Append("static "); } if (field.IsInitOnly) { sb.Append("readonly "); } sb.Append(typeReferencer.GetTypeReference(field.FieldType)); sb.Append(" "); sb.Append(field.Name); return sb.ToString(); }
public string GetSignature(MethodInfo method) { if (method == null) throw new ArgumentNullException("method"); var typeReferencer = new CSharpTypeReferenceProvider(method.ReflectedType); var sb = new StringBuilder(); if (method.Name == "Finalize") { sb.Append("~"); sb.Append(CSharpTypeReferenceProvider.GetBaseName(method.ReflectedType)); sb.Append("()"); return sb.ToString(); } if (!method.ReflectedType.IsInterface) { AppendNonAccessModifiers(method, sb); } var operatorNameBeforeType = false; if (method.IsSpecialName && (method.Name.Equals("op_Explicit") || method.Name.Equals("op_Implicit"))) { sb.Append(Operators[method.Name]); sb.Append(" "); operatorNameBeforeType = true; } sb.Append(typeReferencer.GetTypeReference(method.ReturnType)); if (!operatorNameBeforeType) { sb.Append(" "); if (method.IsSpecialName && Operators.ContainsKey(method.Name)) { sb.Append(Operators[method.Name]); } else { sb.Append(method.Name); } } AppendGenericDeclaration(method, sb, typeReferencer); AppendMethodParameters(method, sb, typeReferencer); return sb.ToString(); }
public string GetSignature(PropertyInfo property) { if (property == null) throw new ArgumentNullException("property"); var typeReferencer = new CSharpTypeReferenceProvider(property.ReflectedType); var sb = new StringBuilder(); var getter = property.GetGetMethod(false); var setter = property.GetSetMethod(false); if (!property.ReflectedType.IsInterface) { var accessor = getter ?? setter; if (accessor != null) { AppendNonAccessModifiers(accessor, sb); } } sb.Append(typeReferencer.GetTypeReference(property.PropertyType)); sb.Append(" "); var indexers = property.GetIndexParameters(); if (indexers.Length > 0) { sb.Append("this["); sb.Append(FormatParameters(indexers, typeReferencer)); sb.Append("]"); } else { sb.Append(property.Name); } sb.Append(" { "); if (getter != null) { sb.Append("get; "); } if (setter != null) { sb.Append("set; "); } sb.Append("}"); return sb.ToString(); }
public string GetSignature(ConstructorInfo constructor) { if (constructor == null) throw new ArgumentNullException("constructor"); var typeReferencer = new CSharpTypeReferenceProvider(constructor.ReflectedType); var sb = new StringBuilder(); AppendNonAccessModifiers(constructor, sb); sb.Append(CSharpTypeReferenceProvider.GetBaseName(constructor.ReflectedType)); AppendMethodParameters(constructor, sb, typeReferencer); return sb.ToString(); }
private static string FormatParameters(IEnumerable <ParameterInfo> parameters, CSharpTypeReferenceProvider typeReferencer) { var paramStrings = parameters .Select(p => string.Format( CultureInfo.InvariantCulture, "{0} {1}", FormatParameter(p, typeReferencer), p.Name)) .ToArray(); return(string.Join(", ", paramStrings)); }