Best Vstest code snippet using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.ManagedNameHelper.AppendTypeString
ManagedNameHelper.Reflection.cs
Source:ManagedNameHelper.Reflection.cs
...108 }109 var typeBuilder = new StringBuilder();110 var methodBuilder = new StringBuilder();111 // Namespace and Type Name (with arity designation)112 var hierarchyPos = AppendTypeString(typeBuilder, semanticType, closedType: false);113 // Method Name with method arity114 var arity = method.GetGenericArguments().Length;115 AppendMethodString(methodBuilder, method.Name, arity);116 if (arity > 0)117 {118 methodBuilder.Append('`');119 methodBuilder.Append(arity);120 }121 // Type Parameters122 var paramList = method.GetParameters();123 if (paramList.Length != 0)124 {125 methodBuilder.Append('(');126 foreach (var p in paramList)127 {128 AppendTypeString(methodBuilder, p.ParameterType, closedType: true);129 methodBuilder.Append(',');130 }131 // Replace the last ',' with ')'132 methodBuilder[methodBuilder.Length - 1] = ')';133 }134 managedTypeName = typeBuilder.ToString();135 managedMethodName = methodBuilder.ToString();136 hierarchyValues = new[] {137 managedTypeName.Substring(hierarchyPos[0], hierarchyPos[1] - hierarchyPos[0]),138 managedTypeName.Substring(hierarchyPos[1] + 1, hierarchyPos[2] - hierarchyPos[1] - 1),139 };140 }141 /// <summary>142 /// Gets the <see cref="MethodBase"/> object with the specified <paramref name="managedTypeName"/>143 /// and <paramref name="managedMethodName"/> in the <paramref name="assembly"/> instance.144 /// </summary>145 /// <param name="assembly">146 /// An <see cref="Assembly" /> instance to search in.147 /// </param>148 /// <param name="managedTypeName">149 /// The fully qualified managed name of the type.150 /// The format is defined in <see href="https://github.com/microsoft/vstest-docs/blob/main/RFCs/0017-Managed-TestCase-Properties.md#managedtype-property">the RFC</see>.151 /// </param>152 /// <param name="managedMethodName">153 /// The fully qualified managed name of the method.154 /// The format is defined in <see href="https://github.com/microsoft/vstest-docs/blob/main/RFCs/0017-Managed-TestCase-Properties.md#managedmethod-property">the RFC</see>.155 /// </param>156 /// <returns>157 /// A <see cref="MethodBase" /> object that represents specified parameters, throws if null.158 /// </returns>159 /// <exception cref="InvalidManagedNameException">160 /// Values specified with <paramref name="managedTypeName"/> and <paramref name="managedMethodName"/>161 /// does not correspond to a method in the <paramref name="assembly"/> instance, or malformed.162 /// </exception>163 /// <remarks>164 /// More information about <paramref name="managedTypeName"/> and <paramref name="managedMethodName"/> can be found in165 /// <see href="https://github.com/microsoft/vstest-docs/blob/main/RFCs/0017-Managed-TestCase-Properties.md">the RFC</see>.166 /// </remarks>167 public static MethodBase GetMethod(Assembly assembly, string managedTypeName, string managedMethodName)168 {169 Type type;170 var parsedManagedTypeName = ReflectionHelpers.ParseEscapedString(managedTypeName);171#if !NETSTANDARD1_0 && !NETSTANDARD1_3 && !WINDOWS_UWP172 type = assembly.GetType(parsedManagedTypeName, throwOnError: false, ignoreCase: false);173#else174 try175 {176 type = assembly.GetType(parsedManagedTypeName);177 }178 catch179 {180 type = null;181 }182#endif183 if (type == null)184 {185 string message = string.Format(CultureInfo.CurrentCulture, Resources.ErrorTypeNotFound, parsedManagedTypeName);186 throw new InvalidManagedNameException(message);187 }188 MethodInfo method = null;189 ManagedNameParser.ParseManagedMethodName(managedMethodName, out var methodName, out var methodArity, out var parameterTypes);190#if NET20 || NET35191 if (!IsNullOrWhiteSpace(methodName))192#else193 if (!string.IsNullOrWhiteSpace(methodName))194#endif195 {196 method = FindMethod(type, methodName, methodArity, parameterTypes);197 }198 if (method == null)199 {200 string message = string.Format(CultureInfo.CurrentCulture, Resources.ErrorMethodNotFound, methodName, managedTypeName);201 throw new InvalidManagedNameException(message);202 }203 return method;204 }205 private static MethodInfo FindMethod(Type type, string methodName, int methodArity, string[] parameterTypes)206 {207 bool filter(MemberInfo mbr, object param)208 {209 var method = mbr as MethodInfo;210 if (method.Name != methodName || method.GetGenericArguments().Length != methodArity)211 {212 return false;213 }214 var paramList = method.GetParameters();215 if (paramList.Length == 0 && parameterTypes == null)216 {217 return true;218 }219 else if (parameterTypes == null || paramList.Length != parameterTypes.Length)220 {221 return false;222 }223 for (int i = 0; i < paramList.Length; i++)224 {225 if (GetTypeString(paramList[i].ParameterType, closedType: true) != parameterTypes[i])226 {227 return false;228 }229 }230 return true;231 }232 MemberInfo[] methods;233#if !NETSTANDARD1_0 && !NETSTANDARD1_3 && !WINDOWS_UWP234 var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;235 methods = type.FindMembers(MemberTypes.Method, bindingFlags, filter, null);236#else237 methods = type.GetRuntimeMethods().Where(m => filter(m, null)).ToArray();238#endif239#if NET20240 return (MethodInfo)SingleOrDefault(methods);241#else242 return (MethodInfo)methods.SingleOrDefault();243#endif244 }245 private static int[] AppendTypeString(StringBuilder b, Type type, bool closedType)246 {247 int[] hierarchies = null;248 if (type.IsArray)249 {250 hierarchies = AppendTypeString(b, type.GetElementType(), closedType);251 b.Append('[');252 for (int i = 0; i < type.GetArrayRank() - 1; i++)253 {254 b.Append(',');255 }256 b.Append(']');257 }258 else if (type.IsGenericParameter)259 {260 if (ReflectionHelpers.GetDeclaringMethod(type) != null)261 {262 b.Append('!');263 }264 b.Append('!');265 b.Append(type.GenericParameterPosition);266 }267 else268 {269 hierarchies = new int[3];270 hierarchies[0] = b.Length;271 AppendNamespace(b, type.Namespace);272 hierarchies[1] = b.Length;273 b.Append('.');274 AppendNestedTypeName(b, type);275 if (closedType)276 {277 AppendGenericTypeParameters(b, type);278 }279 hierarchies[2] = b.Length;280 }281 return hierarchies;282 }283 private static void AppendNamespace(StringBuilder b, string namespaceString)284 {285 int start = 0;286 bool shouldEscape = false;287 for (int i = 0; i <= namespaceString.Length; i++)288 {289 if (i == namespaceString.Length || namespaceString[i] == '.')290 {291 if (start != 0)292 {293 b.Append('.');294 }295 var part = namespaceString.Substring(start, i - start);296 if (shouldEscape)297 {298 NormalizeAndAppendString(b, part);299 shouldEscape = false;300 }301 else302 {303 b.Append(part);304 }305 start = i + 1;306 continue;307 }308 shouldEscape = shouldEscape || NeedsEscaping(namespaceString[i], i - start);309 }310 }311 private static void AppendMethodString(StringBuilder methodBuilder, string name, int methodArity)312 {313 var arityStart = name.LastIndexOf('`');314 var arity = 0;315 if (arityStart > 0)316 {317 arityStart++;318 var arityString = name.Substring(arityStart, name.Length - arityStart);319 if (int.TryParse(arityString, out arity))320 {321 if (arity == methodArity)322 {323 name = name.Substring(0, arityStart - 1);324 }325 }326 }327 if (IsNormalized(name))328 {329 methodBuilder.Append(name);330 }331 else332 {333 NormalizeAndAppendString(methodBuilder, name);334 }335 if (arity > 0 && methodArity == arity)336 {337 methodBuilder.Append($"`{arity}");338 }339 }340 private static void NormalizeAndAppendString(StringBuilder b, string name)341 {342 b.Append('\'');343 for (int i = 0; i < name.Length; i++)344 {345 char c = name[i];346 if (NeedsEscaping(c, i))347 {348 if (c == '\\' || c == '\'')349 {350 // var encoded = Convert.ToString(((uint)c), 16);351 // b.Append("\\u");352 // b.Append('0', 4 - encoded.Length);353 // b.Append(encoded);354 b.Append('\\');355 b.Append(c);356 continue;357 }358 }359 b.Append(c);360 }361 b.Append('\'');362 }363 private static int AppendNestedTypeName(StringBuilder b, Type type)364 {365 var outerArity = 0;366 if (type.IsNested)367 {368 outerArity = AppendNestedTypeName(b, type.DeclaringType);369 b.Append('+');370 }371 var typeName = type.Name;372 var stars = 0;373 if (type.IsPointer)374 {375 for (int i = typeName.Length - 1; i > 0; i--)376 {377 if (typeName[i] != '*')378 {379 stars = typeName.Length - i - 1;380 typeName = typeName.Substring(0, i + 1);381 break;382 }383 }384 }385 var info = type.GetTypeInfo();386 var arity = !info.IsGenericType387 ? 0388 : info.GenericTypeParameters.Length > 0389 ? info.GenericTypeParameters.Length390 : info.GenericTypeArguments.Length;391 AppendMethodString(b, typeName, arity - outerArity);392 b.Append('*', stars);393 return arity;394 }395 private static void AppendGenericTypeParameters(StringBuilder b, Type type)396 {397 Type[] genargs;398#if !NETSTANDARD1_0 && !NETSTANDARD1_3 && !WINDOWS_UWP399 genargs = type.GetGenericArguments();400#else401 genargs = type.GetTypeInfo().GenericTypeArguments;402#endif403 if (genargs.Length != 0)404 {405 b.Append('<');406 foreach (var argType in genargs)407 {408 AppendTypeString(b, argType, closedType: true);409 b.Append(',');410 }411 // Replace the last ',' with '>'412 b[b.Length - 1] = '>';413 }414 }415 private static bool IsNormalized(string s)416 {417 for (int i = 0; i < s.Length; i++)418 {419 if (NeedsEscaping(s[i], i) && s[i] != '.')420 {421 return false;422 }423 }424 return true;425 }426 private static bool NeedsEscaping(char c, int pos)427 {428 if (pos == 0 && char.IsDigit(c))429 {430 return true;431 }432 if (c == '_'433 || char.IsLetterOrDigit(c) // Lu, Ll, Lt, Lm, Lo, or Nl434 )435 {436 return false;437 }438 var category = CharUnicodeInfo.GetUnicodeCategory(c);439 if (category == UnicodeCategory.NonSpacingMark // Mn440 || category == UnicodeCategory.SpacingCombiningMark // Mc441 || category == UnicodeCategory.ConnectorPunctuation // Pc442 || category == UnicodeCategory.Format) // Cf443 {444 return false;445 }446 return true;447 }448 private static string GetTypeString(Type type, bool closedType)449 {450 var builder = new StringBuilder();451 AppendTypeString(builder, type, closedType);452 return builder.ToString();453 }454#if NET20455 // the method is mostly copied from456 // https://github.com/dotnet/runtime/blob/c0840723b382bcfa67b35839af8572fcd38f1d13/src/libraries/System.Linq/src/System/Linq/Single.cs#L86457 public static TSource SingleOrDefault<TSource>(System.Collections.Generic.IEnumerable<TSource> source)458 {459 if (source == null)460 {461 throw new ArgumentNullException(nameof(source));462 }463 if (source is System.Collections.Generic.IList<TSource> list)464 {465 switch (list.Count)...
AppendTypeString
Using AI Code Generation
1using System;2using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;3{4 {5 public static void Main(string[] args)6 {7 ManagedNameHelper.AppendTypeString(typeof(Program), null);8 }9 }10}11using System;12using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;13{14 {15 public static void Main(string[] args)16 {17 ManagedNameHelper.AppendTypeString(typeof(Program), null);18 }19 }20}21using System;22using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;23{24 {25 public static void Main(string[] args)26 {27 ManagedNameHelper.AppendTypeString(typeof(Program), null);28 }29 }30}31using System;32using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;33{34 {35 public static void Main(string[] args)36 {37 ManagedNameHelper.AppendTypeString(typeof(Program), null);38 }39 }40}41using System;42using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;43{44 {45 public static void Main(string[] args)46 {47 ManagedNameHelper.AppendTypeString(typeof(Program), null);48 }49 }50}51using System;52using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;53{54 {55 public static void Main(string[] args)56 {57 ManagedNameHelper.AppendTypeString(typeof(Program), null);58 }59 }60}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!