private static string NonStaticProperty(MemberInfo propertyOrField, Type currentType, bool hasGet, bool hasSet) { var typeFullNameWithDynamic = TypeFullName.GetTypeFullName_WithDynamic(currentType); string nonstaticGet = string.Empty; if (hasGet) { nonstaticGet = GetNonStaticGet(propertyOrField, currentType); } string nonstaticSet = string.Empty; if (hasSet) { nonstaticSet = GetNonStaticSet(propertyOrField, currentType); } return($@" public {typeFullNameWithDynamic} {propertyOrField.Name} {{ {nonstaticGet} {nonstaticSet} }} "); }
private static string GetNonStaticGet(MemberInfo propertyOrField, Type propertyOrFieldType) { string getCode = string.Empty; if (Converters.HaveToBeConverted(propertyOrFieldType)) { getCode = $@" var value = teklaObject.{propertyOrField.Name}; {Converters.FromTSObjects(propertyOrFieldType, "value", "var value_")} return ({TypeFullName.GetTypeFullName_WithDynamic(propertyOrFieldType)}) value_;"; } else { getCode = $@" return teklaObject.{ propertyOrField.Name};"; } return($@"get {{ try {{{getCode} }} catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex) {{ throw DynamicAPINotFoundException.CouldNotFindProperty(nameof({propertyOrField.Name}), ex); }} }}"); }
public void EmptyStringIsNotAValidTypeName() { Assert_Throws <ArgumentException>(() => TypeFullName.Parse(null)); Assert_Throws <ArgumentException>(() => TypeFullName.Parse(string.Empty)); Assert_Throws <ArgumentException>(() => TypeFullName.Parse(" ")); Assert_Throws <ArgumentException>(() => TypeFullName.Parse(" \t\r\n ")); }
public void AssemblyQualifiedTypeNameContainsAssemblyPart() { var tn = TypeFullName.Parse("System.String, mscorlib"); Assert.AreEqual("System.String", tn.TypeName); Assert.AreEqual("mscorlib", tn.AssemblyName); }
public static bool TryGetTypeFullNameAndMember(this XmlAttribute attr, out TypeFullName typeFullName, out RawString memberName) { if (attr.TryGetFullName(out var ns, out var typeAndMember) == false) { goto FAILURE; } var i = typeAndMember.LastIndexOf((byte)'.'); if (i < 0) { goto FAILURE; } var typeName = typeAndMember.Slice(0, i); memberName = typeAndMember.Slice(i + 1); if (typeName.IsEmpty || memberName.IsEmpty) { goto FAILURE; } typeFullName = new TypeFullName(ns, typeName); return(true); FAILURE: typeFullName = default; memberName = default; return(false); }
public static string ToTSObjects(Type type, string inputName, string outputName) { var typeFullName = TypeFullName.GetTypeFullName(type); var typeFullNameWithDynamic = TypeFullName.GetTypeFullName_WithDynamic(type); if (TypeFullName.IsTeklaType(type)) { if (typeFullNameWithDynamic.EndsWith("[]", StringComparison.InvariantCulture)) { return(outputName + " = " + typeFullNameWithDynamic.Replace("[]", "") + "Array_.GetTSObject(" + inputName + ");"); } else { return(outputName + " = " + typeFullNameWithDynamic + "_.GetTSObject(" + inputName + ");"); } } else if (typeof(ArrayList).IsAssignableFrom(type)) { return(outputName + " = ArrayListConverter.ToTSObjects(" + inputName + ");"); } else if (typeFullName.StartsWith("System.Collections.Generic.List<", StringComparison.InvariantCulture) || typeFullName.StartsWith("System.Collections.Generic.IList<", StringComparison.InvariantCulture) ) { if (typeFullName.StartsWith("System.Collections.Generic.List<System.Collections.Generic.List<", StringComparison.InvariantCulture) || typeFullName.StartsWith("System.Collections.Generic.IList<System.Collections.Generic.IList<", StringComparison.InvariantCulture)) { return(outputName + " = ListOfListConverter.ToTSObjects(" + inputName + ");"); } else { return(outputName + " = ListConverter.ToTSObjects(" + inputName + ");"); } } else if (typeof(System.Type).IsAssignableFrom(type) || typeof(System.Type[]).IsAssignableFrom(type)) { return(outputName + " = TypeConverter.ToTSObjects(" + inputName + ");"); } else if (typeof(IEnumerable).IsAssignableFrom(type)) { //var ienumerableParameters = typeFullName.Substring(typeFullName.IndexOf("<"), typeFullName.Length - typeFullName.IndexOf("<")); return(outputName + " = IEnumerableConverter.ToTSObjects(" + inputName + ");"); } else if (typeFullName.StartsWith("System.Tuple", StringComparison.InvariantCulture)) { var tupleParams = typeFullName.Substring(typeFullName.IndexOf("<"), typeFullName.Length - typeFullName.IndexOf("<")); return(outputName + " = TupleConverter.ToTSObjects" + tupleParams + "(" + inputName + ");"); } else if (typeFullName.StartsWith("System.Nullable", StringComparison.InvariantCulture)) { return(outputName + " = NullableConverter.ToTSObjects(" + inputName + ");"); } else { return(outputName + " = ObjectConverter.ToTSObject(" + inputName + ");"); } }
public void Dictionary2() { var type = typeof(Dictionary <string, Beam.BeamTypeEnum>); Console.WriteLine(type.ToString()); var result = TypeFullName.GetTypeFullName(type); Console.WriteLine("Result:\t" + result); Assert.AreEqual("System.Collections.Generic.Dictionary<System.String, Tekla.Structures.Model.Beam.BeamTypeEnum>", result); }
public void TSM_Beam_BeamTypeEnum() { var type = typeof(Tekla.Structures.Model.Beam.BeamTypeEnum); Console.WriteLine(type.ToString()); var result = TypeFullName.GetTypeFullName(type); Console.WriteLine("Result:\t" + result); Assert.AreEqual("Tekla.Structures.Model.Beam.BeamTypeEnum", result); }
public void Int32() { var type = typeof(int[]); Console.WriteLine(type.ToString()); var result = TypeFullName.GetTypeFullName(type); Console.WriteLine("Result:\t" + result); Assert.AreEqual("System.Int32[]", result); }
public void GenericList() { var type = typeof(List <string>); Console.WriteLine(type.ToString()); var result = TypeFullName.GetTypeFullName(type); Console.WriteLine("Result:\t" + result); Assert.AreEqual("System.Collections.Generic.List<System.String>", result); }
private static string GetReturnType(MethodInfo methodInfo) { var returnType = TypeFullName.GetTypeFullName_WithDynamic(methodInfo.ReturnType); if (returnType.Equals("System.Void")) { returnType = "void"; } return(returnType); }
public void AssemblyQualifiedTypeNameContainsAssemblyAndVersionParts() { var tn = TypeFullName.Parse(typeof(string).AssemblyQualifiedName); Assert.AreEqual("System.String", tn.TypeName); Assert.AreEqual("mscorlib", tn.AssemblyName); tn = TypeFullName.Parse("System.Management.Automation.PSObject, System.Management.Automation, Version=3.0.0.0"); Assert.AreEqual("System.Management.Automation.PSObject", tn.TypeName); Assert.AreEqual("System.Management.Automation", tn.AssemblyName); }
public void Nullable2() { var type = typeof(System.Nullable <DateTime>); Console.WriteLine(type.ToString()); var result = TypeFullName.GetTypeFullName(type); Console.WriteLine("Result:\t" + result); Assert.AreEqual("System.Nullable<System.DateTime>", result); }
public void Dictionary3() { var type = typeof(Dictionary <string, Dictionary <string, int[]> >); Console.WriteLine(type.ToString()); var result = TypeFullName.GetTypeFullName(type); Console.WriteLine("Result:\t" + result); Assert.AreEqual("System.Collections.Generic.Dictionary<System.String, System.Collections.Generic.Dictionary<System.String, System.Int32[]>>", result); }
public void DetailMark_DetailMarkAttributes() { var type = typeof(Tekla.Structures.Drawing.DetailMark.DetailMarkAttributes.DetailBoundaryShape); Console.WriteLine(type.ToString()); var result = TypeFullName.GetTypeFullName(type); Console.WriteLine("Result:\t" + result); Assert.AreEqual("Tekla.Structures.Drawing.DetailMark.DetailMarkAttributes.DetailBoundaryShape", result); }
private static string GenerateFullNameFromType(Type type) { TypeFullName nameWithoutArguments = TypeFullNameParser.Parse(type.FullName); TypeFullName result = new TypeFullName( nameWithoutArguments.FullName, type.GetGenericArguments() .Select((genericType) => new TypeFullName(genericType.FullName ?? genericType.Name)) .ToArray()); return(result.ToString()); }
public bool IsPropertyNode(XmlNode node, [MaybeNullWhen(false)] out TypeData propOwnerType, out RawString propName) { var(nsName, tmp) = node.GetFullName(); (var name, propName) = tmp.Split2((byte)'.'); var typeName = new TypeFullName(nsName, name).ToString(); if (propName.IsEmpty || TryGetTypeData(typeName, out propOwnerType) == false) { propOwnerType = null; return(false); } return(true); }
public override string StartType(TypeInfo type, bool showAttributes, string attributes) { var objectType = GetObjectType(); var staticObject = type.IsAbstract && type.IsSealed ? "<< static >> " : string.Empty; return(@$ " {objectType} " "{DisplayName}" " as {TypeFullName.AsSlug()} {objectType} {TypeFullName.AsSlug()} {staticObject}{{" + (showAttributes ? $"\n\t--- attributes ---\n{attributes}" : string.Empty)); }
public void Dictionary_In_DatabaseObject() { var method = typeof(Tekla.Structures.Drawing.DatabaseObject).GetMethods() .Where(m => m.Name.Equals("GetStringUserProperties", StringComparison.InvariantCulture) && m.GetParameters().Count().Equals(1)).FirstOrDefault(); Assert.NotNull(method); var param = method.GetParameters().FirstOrDefault(); var result = TypeFullName.GetTypeFullName(param.ParameterType); Console.WriteLine(result); Assert.AreEqual("System.Collections.Generic.Dictionary<System.String, System.String>", result); }
private void CreateFields() { string typeName = TypeFullName.Parse(ModelSelectedTemplate); TemplateConfiguration templateConfig = TemplateConfiguration.GetConfiguration(GetService <DTE>(true)); foreach (ModelProvider modelProvider in templateConfig.ModelProviders) { Assembly modelAssembly = Assembly.LoadFrom(modelProvider.ProviderAssemblyLocation); Type modelType = modelAssembly.GetType(typeName); if (modelType != null) { TraslateFields(modelType); break; } } }
private string GetFieldsText(Type type) { var sb = new StringBuilder(100); var fields = GetFields(type); foreach (var field in fields) { sb.Append("\t\t\t"); sb.Append("public "); sb.Append(TypeFullName.GetTypeFullName_WithDynamic(field.FieldType)); sb.Append(" "); sb.Append(field.Name); sb.Append(";\n"); } return(sb.ToString()); }
private static string GetStaticGet(MemberInfo propertyOrField, Type propertyOrFieldType) { var propTypeFullName = TypeFullName.GetTypeFullName_WithDynamic(propertyOrFieldType); string valueConverter = $"return ({propTypeFullName}) value;"; if (Converters.HaveToBeConverted(propertyOrFieldType)) { valueConverter = Converters.FromTSObjects(propertyOrFieldType, "value", "var value_"); valueConverter += $"\n\treturn ({propTypeFullName}) value_;"; } return($@"get {{ var value = PropertyInvoker.GetStaticPropertyOrFieldValue(""$typeFullName"", ""{propertyOrField.Name}""); {valueConverter} }}"); }
/// <summary> /// Gets the name of the type as if it was written in code. (Useful for debugging) /// </summary> /// <param name="type">The type for which to get name</param> /// <param name="returnTypeFullName">Whether to return type full name or not, default is <see cref="TypeFullName.FullNameForUncommonTypes"/></param> /// <returns></returns> public static string GetFriendlyName(this Type type, TypeFullName returnTypeFullName = TypeFullName.FullNameForUncommonTypes) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (type.IsArray) { var elementType = type.GetElementType(); var friendlyName = elementType.GetFriendlyName(returnTypeFullName); return(friendlyName + "[]"); } var info = type.GetTypeInfo(); if (!info.IsGenericType) { return(GetTypeName(type, returnTypeFullName)); } // type should be generic if (type.GetGenericTypeDefinition() == typeof(Nullable <>)) { return($"{type.GetGenericArguments().First().GetFriendlyName(returnTypeFullName)}?"); } string name = GetTypeName(type, returnTypeFullName); int index = name.IndexOf('`'); name = name.Substring(0, index) + "<"; var args = type.GetGenericArguments(); for (int i = 0; i < args.Length; i++) { name += args[i].GetFriendlyName(returnTypeFullName); if (i < args.Length - 1) { name += ", "; } } name += ">"; return(name); }
private static string GetTypeName(Type type, TypeFullName returnTypeFullName) { string name = type.Name; if (returnTypeFullName == TypeFullName.FullName) { name = $"{type.Namespace}.{type.Name}"; } else if (returnTypeFullName == TypeFullName.FullNameForUncommonTypes) { if (!_commonNamespaces.Contains(type.Namespace)) { name = $"{type.Namespace}.{type.Name}"; } } // TypeFullName.ShortName is left return(name); }
public override int GetHashCode() { int hash = 1; if (TypeFullName.Length != 0) { hash ^= TypeFullName.GetHashCode(); } if (PropertyName.Length != 0) { hash ^= PropertyName.GetHashCode(); } if (ForeignKeyName.Length != 0) { hash ^= ForeignKeyName.GetHashCode(); } return(hash); }
public void SimpleTypeNameDoesntHaveAssemblyAndVersionParts() { var tn = TypeFullName.Parse("string"); Assert.AreEqual("string", tn.TypeName); Assert.AreEqual("mscorlib", tn.AssemblyName); tn = TypeFullName.Parse("System.String"); Assert.AreEqual("System.String", tn.TypeName); Assert.AreEqual("mscorlib", tn.AssemblyName); tn = TypeFullName.Parse("System.Management.Automation.PSObject"); Assert.AreEqual("System.Management.Automation.PSObject", tn.TypeName); Assert.AreEqual("mscorlib", tn.AssemblyName); tn = TypeFullName.Parse(typeof(Enumerable).FullName); Assert.AreEqual("System.Linq.Enumerable", tn.TypeName); Assert.AreEqual("mscorlib", tn.AssemblyName); }
public string GetTextFromType(Type type) { if (type.IsEnum == false) { return(string.Empty); } var sb = new StringBuilder(text.Length * 5); sb.Append(text); sb.Replace("$enumValues", GetEnumValuesText(type)); sb.Replace("$switch1Values", GetSwitch1Values(type)); sb.Replace("$switch2Values", GetSwitch2Values(type)); sb.Replace("$classname", type.Name); sb.Replace("$typeFullName", TypeFullName.GetTypeFullName_WithDynamic(type).Replace("Dynamic.", "")); return(sb.ToString()); }
private static string FormatTemplate(IEnumerable <string> objectValues) { var formattedObject = string.Empty; foreach (string item in objectValues) { string formattedItemName = TypeFullName.Parse(item); if (formattedItemName.StartsWith(".")) { formattedItemName = formattedItemName.Remove(0, 1); } if (formattedObject == string.Empty) { formattedObject = "'" + formattedItemName + "'"; } else { formattedObject = formattedObject + ",\r\n\t\t'" + formattedItemName + "'"; } } return(formattedObject); }
private static bool TryGetBuilderName(XmlObject xml, [MaybeNullWhen(false)] out TypeFullName builderName, out Diagnostic?diagnostic) { // x-Builder="Namespace.Name" if (xml.Root.TryFindAttribute("x-Builder", out var attr) == false) { builderName = TypeFullName.Empty; diagnostic = DiagnosticHelper.BuilderNotSpecified(); return(false); } var value = attr.Value; var index = value.LastIndexOf((byte)'.'); if (index < 0) { builderName = TypeFullName.Empty; diagnostic = DiagnosticHelper.InvalidBuilderName(value.ToString()); return(false); } builderName = new TypeFullName(value.Slice(0, index), value.Slice(index + 1)); diagnostic = null; return(true); }
public static System.Type FromTSObjects(System.Type input) { try { var typeFullName = TypeFullName.GetTypeFullName(input); if (typeFullName.StartsWith("Tekla.Structures.", System.StringComparison.InvariantCulture)) { var typeName = typeFullName.Replace("Tekla.Structures.", "Dynamic.Tekla.Structures."); return(TSActivator.GetTypeFromTypeName(typeName)); } else { return(input); } } catch (System.Exception ex) { throw new DynamicAPIException("Error in method TypeConverter.FromTSObjects() Input type: " + input.GetType().ToString() + "\n Internal error message: " + ex.Message, ex); } }