private static MethodInfo GetConvOp(Type t1, Type t2) { lock (conv_ops_lock) { if (!conv_ops.ContainsKey(t1)) { conv_ops.Add(t1, new Dictionary <Type, MethodInfo>()); } } ITypeInfo typeInfo = TypeFactory.GetTypeInfo(t1); ITypeInfo typeInfo2 = TypeFactory.GetTypeInfo(t2); if (conv_ops[t1].ContainsKey(t2)) { return(conv_ops[t1][t2]); } MethodInfo method = typeInfo.GetMethod("op_Implicit", new ITypeInfo[1] { typeInfo2 }); lock (conv_ops_lock) { try { conv_ops[t1].Add(t2, method); return(method); } catch (ArgumentException) { return(conv_ops[t1][t2]); } } }
public XunitMethodInfo GetMethod(string methodName) { IMethodInfo method = target.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); return(method != null ? new XunitMethodInfoAdapter(method) : null); }
private GallioFunc <object[], TOutput> TryGetMemberAsMethod(FixtureMemberInvokerTargets targets, ITypeInfo ownerInfo) { if ((targets & FixtureMemberInvokerTargets.Method) != 0) { IMethodInfo info = ownerInfo.GetMethod(memberName, bindingFlags); if (info != null) { return(args => { object fixtureInstance = GetFixtureInstance(info.IsStatic); MethodInfo method = (type == null) ? GetMemberInfo <MethodInfo>(t => t.GetMethod(memberName, bindingFlags)) : info.Resolve(true); if (method == null) { throw new TestFailedException(String.Format("Could not find method '{0}'.", memberName)); } object[] convertedArgs = ConvertArguments(method.GetParameters(), args); return (TOutput)method.Invoke(fixtureInstance, convertedArgs); }); } } return(null); }
public void CanFindPrivateStaticMethod() { ITypeInfo typeInfo = Reflector.Wrap(typeof(TestClass)); IMethodInfo result = typeInfo.GetMethod("PrivateStaticMethod"); Assert.NotNull(result); }
public void NonExistantMethodReturnsNull() { ITypeInfo typeInfo = Reflector.Wrap(typeof(TestClass)); IMethodInfo result = typeInfo.GetMethod("NonExistantMethod"); Assert.Null(result); }
public void InvokeThrowsUnwrappedException() { ITypeInfo type = Reflector.Wrap(typeof(NonGenericClass)); IMethodInfo method = type.GetMethod("StaticMethodThatThrows", PublicStatic); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(NonGenericClass), method, slotValues, NullConverter.Instance); Assert.Throws <InvalidOperationException>(delegate { spec.Invoke(null); }); }
public void FormatDescribesTheMethod() { ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>)); IMethodInfo method = type.GetMethod("StaticMethod", PublicStatic); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); slotValues.Add((IGenericParameterInfo)method.GenericArguments[0], typeof(int)); slotValues.Add(method.Parameters[0], 1); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(GenericClass <int>), method, slotValues, NullConverter.Instance); Assert.AreEqual("Foo<int>(1)", spec.Format("Foo", RuntimeAccessor.ServiceLocator.Resolve <IFormatter>())); }
public void InvokeStaticMethodWithGenericMethodInstantiation() { ITypeInfo type = Reflector.Wrap(typeof(GenericClass <int>)); IMethodInfo method = Reflector.Wrap(type.GetMethod("StaticMethod", PublicStatic).Resolve(true).MakeGenericMethod(typeof(int))); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); slotValues.Add(method.Parameters[0], 1); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(GenericClass <int>), method, slotValues, NullConverter.Instance); spec.Invoke(null); Assert.AreEqual(typeof(int), GenericClass <int> .staticMethodTypeValue); Assert.AreEqual(1, GenericClass <int> .staticMethodParamValue); }
private static PaginatedResource <ItemType> Create <ItemType, RequestType, ResponseType> (object client, string methodName, object request, string tokenRequestPropertyPath, string tokenResponsePropertyPath, string itemListPropertyPath) { ITypeInfo clientType = TypeFactory.GetTypeInfo(client.GetType()); MethodInfo fetcherMethod = clientType.GetMethod(methodName, new ITypeInfo[] { TypeFactory.GetTypeInfo(typeof(RequestType)) }); Func <RequestType, ResponseType> call = (req) => { return((ResponseType)fetcherMethod.Invoke(client, new object[] { req })); }; return(Create <ItemType, RequestType, ResponseType>(call, (RequestType)request, tokenRequestPropertyPath, tokenResponsePropertyPath, itemListPropertyPath)); }
private static PaginatedResource <ItemType> Create <ItemType, TRequestType, TResponseType>(object client, string methodName, object request, string tokenRequestPropertyPath, string tokenResponsePropertyPath, string itemListPropertyPath) { ITypeInfo typeInfo = TypeFactory.GetTypeInfo(client.GetType()); MethodInfo fetcherMethod = typeInfo.GetMethod(methodName, new ITypeInfo[1] { TypeFactory.GetTypeInfo(typeof(TRequestType)) }); GetFuncType <TRequestType, TResponseType>(); return(Create <ItemType, TRequestType, TResponseType>((TRequestType req) => (TResponseType)fetcherMethod.Invoke(client, new object[1] { req }), (TRequestType)request, tokenRequestPropertyPath, tokenResponsePropertyPath, itemListPropertyPath)); }
private static RequestedAction CallProcessFinalBitmap(BuilderExtension plugin, ImageState state) { ITypeInfo type = Reflector.Wrap(plugin.GetType()); IMethodInfo method = type.GetMethod("ProcessFinalBitmap", BindingFlags.Instance | BindingFlags.NonPublic); Assert.IsNotNull(method, "Did not find 'ProcessFinalBitmap' method on plugin."); MethodInfo m = method.Resolve(false); Assert.IsNotNull(m); RequestedAction requestedAction = (RequestedAction)m.Invoke(plugin, new object[] { state }); return(requestedAction); }
public void InvokeInstanceMethodWithGenericMethodOnGenericTypeDefn() { ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>)); IMethodInfo method = type.GetMethod("InstanceMethod", PublicInstance); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); slotValues.Add((IGenericParameterInfo)method.GenericArguments[0], typeof(int)); slotValues.Add(method.Parameters[0], 1); GenericClass <int> instance = new GenericClass <int>(); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(GenericClass <int>), method, slotValues, NullConverter.Instance); spec.Invoke(instance); Assert.AreEqual(typeof(int), instance.instanceMethodTypeValue); Assert.AreEqual(1, instance.instanceMethodParamValue); }
public void SpecPropertiesDescribeTheMethod() { ITypeInfo type = Reflector.Wrap(typeof(GenericClass <>)); IMethodInfo method = type.GetMethod("StaticMethod", PublicStatic); Dictionary <ISlotInfo, object> slotValues = new Dictionary <ISlotInfo, object>(); slotValues.Add((IGenericParameterInfo)method.GenericArguments[0], typeof(int)); slotValues.Add(method.Parameters[0], 1); MethodInvocationSpec spec = new MethodInvocationSpec(typeof(GenericClass <int>), method, slotValues, NullConverter.Instance); Assert.AreEqual(typeof(GenericClass <int>), spec.ResolvedType); Assert.AreSame(method, spec.Method); Assert.AreSame(slotValues, spec.SlotValues); Assert.AreSame(NullConverter.Instance, spec.Converter); Assert.AreEqual("Void StaticMethod[Int32](Int32)", spec.ResolvedMethod.ToString()); Assert.AreElementsEqual(new object[] { 1 }, spec.ResolvedArguments); }
public RunTests(Executor executor, string _type, List <string> _methods, object _handler) { Guard.ArgumentNotNull("_type", _type); Guard.ArgumentNotNull("_methods", _methods); ExecutorCallback handler = ExecutorCallback.Wrap(_handler); Type realType = executor.assembly.GetType(_type); Guard.ArgumentValid("_type", "Type " + _type + " could not be found", realType != null); ITypeInfo type = Reflector.Wrap(realType); ITestClassCommand testClassCommand = TestClassCommandFactory.Make(type); List <IMethodInfo> methods = new List <IMethodInfo>(); foreach (string _method in _methods) { try { IMethodInfo method = type.GetMethod(_method); Guard.ArgumentValid("_methods", "Could not find method " + _method + " in type " + _type, method != null); methods.Add(method); } catch (AmbiguousMatchException) { throw new ArgumentException("Ambiguous method named " + _method + " in type " + _type); } } if (testClassCommand == null) { ClassResult result = new ClassResult(type.Type); OnTestResult(result, handler); return; } executor.RunOnSTAThreadWithPreservedWorkingDirectory(() => TestClassCommandRunner.Execute(testClassCommand, methods, command => OnTestStart(command, handler), result => OnTestResult(result, handler))); }
/// <summary> /// Parses a code element from an NUnit test case name. /// The name generally consists of the fixture type full-name followed by /// a dot and the test method name. /// </summary> private static ICodeElementInfo ParseTestCaseName(IAssemblyInfo assembly, string name) { if (assembly != null) { // Handle row-test naming scheme. int firstParen = name.IndexOf('('); if (firstParen >= 0) { name = name.Substring(0, firstParen); } // Parse the identifier. if (IsProbableIdentifier(name)) { int lastDot = name.LastIndexOf('.'); if (lastDot > 0 && lastDot < name.Length - 1) { string typeName = name.Substring(0, lastDot); string methodName = name.Substring(lastDot + 1); ITypeInfo type = assembly.GetType(typeName); if (type != null) { try { return(type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)); } catch (AmbiguousMatchException) { // We may have insufficient information to distinguish overloaded // test methods. In this case we give up trying to find the code element. } } } } } return(null); }
/// <summary> /// This should be a one time call to use reflection to find all the types and methods /// needed for the logging API. /// </summary> private static void loadStatics() { lock (InternalLog4netLogger.LOCK) { if (loadState != LoadState.Uninitialized) return; loadState = LoadState.Loading; try { loggerType = Type.GetType("Amazon.Runtime.Internal.Util.Logger"); // The LogManager and its methods logMangerType = Type.GetType("log4net.Core.LoggerManager, log4net"); logMangerTypeInfo = TypeFactory.GetTypeInfo(logMangerType); if (logMangerType == null) { loadState = LoadState.Failed; return; } getLoggerWithTypeMethod = logMangerTypeInfo.GetMethod("GetLogger", new ITypeInfo[] { TypeFactory.GetTypeInfo(typeof(Assembly)), TypeFactory.GetTypeInfo(typeof(Type)) }); // The ILog and its methdods logType = Type.GetType("log4net.Core.ILogger, log4net"); logTypeInfo = TypeFactory.GetTypeInfo(logType); levelType = Type.GetType("log4net.Core.Level, log4net"); levelTypeInfo = TypeFactory.GetTypeInfo(levelType); debugLevelPropertyValue = levelTypeInfo.GetField("Debug").GetValue(null); infoLevelPropertyValue = levelTypeInfo.GetField("Info").GetValue(null); errorLevelPropertyValue = levelTypeInfo.GetField("Error").GetValue(null); systemStringFormatType = Type.GetType("log4net.Util.SystemStringFormat, log4net"); logMethod = logTypeInfo.GetMethod("Log", new ITypeInfo[] { TypeFactory.GetTypeInfo(typeof(Type)), levelTypeInfo, TypeFactory.GetTypeInfo(typeof(object)), TypeFactory.GetTypeInfo(typeof(Exception)) }); isEnabledForMethod = logTypeInfo.GetMethod("IsEnabledFor", new ITypeInfo[] { levelTypeInfo }); if (getLoggerWithTypeMethod == null || isEnabledForMethod == null || logType == null || levelType == null || logMethod == null) { loadState = LoadState.Failed; return; } // If log4net logging is enabled, we attempt to activate log4net by calling XmlConfigurator.Configure() if ((AWSConfigs.LoggingConfig.LogTo & LoggingOptions.Log4Net) == LoggingOptions.Log4Net) { ITypeInfo xmlConfiguratorType = TypeFactory.GetTypeInfo(Type.GetType("log4net.Config.XmlConfigurator, log4net")); if (xmlConfiguratorType != null) { MethodInfo configureMethod = xmlConfiguratorType.GetMethod("Configure", new ITypeInfo[0]); if (configureMethod != null) { configureMethod.Invoke(null, null); } } } loadState = LoadState.Success; } catch { // Mark as failed so no attempted will be made on the logging methods. loadState = LoadState.Failed; } } }
/// <summary> /// This should be a one time call to use reflection to find all the types and methods /// needed for the logging API. /// </summary> private static void loadStatics() { lock (InternalLog4netLogger.LOCK) { if (loadState != LoadState.Uninitialized) { return; } loadState = LoadState.Loading; try { loggerType = Type.GetType("Amazon.Runtime.Internal.Util.Logger"); // The LogManager and its methods logMangerType = Type.GetType("log4net.Core.LoggerManager, log4net"); logMangerTypeInfo = TypeFactory.GetTypeInfo(logMangerType); if (logMangerType == null) { loadState = LoadState.Failed; return; } getLoggerWithTypeMethod = logMangerTypeInfo.GetMethod("GetLogger", new ITypeInfo[] { TypeFactory.GetTypeInfo(typeof(Assembly)), TypeFactory.GetTypeInfo(typeof(Type)) }); // The ILog and its methdods logType = Type.GetType("log4net.Core.ILogger, log4net"); logTypeInfo = TypeFactory.GetTypeInfo(logType); levelType = Type.GetType("log4net.Core.Level, log4net"); levelTypeInfo = TypeFactory.GetTypeInfo(levelType); debugLevelPropertyValue = levelTypeInfo.GetField("Debug").GetValue(null); infoLevelPropertyValue = levelTypeInfo.GetField("Info").GetValue(null); errorLevelPropertyValue = levelTypeInfo.GetField("Error").GetValue(null); systemStringFormatType = Type.GetType("log4net.Util.SystemStringFormat, log4net"); logMethod = logTypeInfo.GetMethod("Log", new ITypeInfo[] { TypeFactory.GetTypeInfo(typeof(Type)), levelTypeInfo, TypeFactory.GetTypeInfo(typeof(object)), TypeFactory.GetTypeInfo(typeof(Exception)) }); isEnabledForMethod = logTypeInfo.GetMethod("IsEnabledFor", new ITypeInfo[] { levelTypeInfo }); if (getLoggerWithTypeMethod == null || isEnabledForMethod == null || logType == null || levelType == null || logMethod == null) { loadState = LoadState.Failed; return; } // If log4net logging is enabled, we attempt to activate log4net by calling XmlConfigurator.Configure() if ((AWSConfigs.LoggingConfig.LogTo & LoggingOptions.Log4Net) == LoggingOptions.Log4Net) { ITypeInfo xmlConfiguratorType = TypeFactory.GetTypeInfo(Type.GetType("log4net.Config.XmlConfigurator, log4net")); if (xmlConfiguratorType != null) { MethodInfo configureMethod = xmlConfiguratorType.GetMethod("Configure", new ITypeInfo[0]); if (configureMethod != null) { configureMethod.Invoke(null, null); } } } loadState = LoadState.Success; } catch { // Mark as failed so no attempted will be made on the logging methods. loadState = LoadState.Failed; } } }
private Test BuildAssemblyTest_Native(IAssemblyInfo assembly, string location) { if (String.IsNullOrEmpty(location)) { throw new ArgumentNullException("location"); } // If csUnit.Core is not in the GAC then it must be in the assembly path due to a // bug in csUnit setting up the AppDomain. It sets the AppDomain's base directory // to the assembly directory and sets its PrivateBinPath to the csUnit directory // which is incorrect (PrivateBinPath only specifies directories relative to the app base) // so it does actually not ensure that csUnit.Core can be loaded as desired. Assembly csUnitCoreAssembly = typeof(csUnit.Core.Loader).Assembly; if (!csUnitCoreAssembly.GlobalAssemblyCache) { string csUnitAppBase = Path.GetDirectoryName(location); string csUnitCoreAssemblyPathExpected = Path.Combine(csUnitAppBase, "csUnit.Core.dll"); if (!File.Exists(csUnitCoreAssemblyPathExpected)) { return(CreateAssemblyTest(assembly, location, delegate(Test assemblyTest) { TestModel.AddAnnotation(new Annotation(AnnotationType.Error, null, string.Format("Cannot load csUnit tests from '{0}'. " + "'csUnit.Core.dll' and related DLLs must either be copied to the same directory as the test assembly or must be installed in the GAC.", location))); })); } } // Load the assembly using the native CSUnit loader. using (Loader loader = new Loader(location)) { // Construct the test tree return(CreateAssemblyTest(assembly, location, delegate(Test assemblyTest) { TestFixtureInfoCollection collection = loader.TestFixtureInfos; if (collection == null) { return; } foreach (ITestFixtureInfo fixtureInfo in collection) { try { ITypeInfo fixtureType = assembly.GetType(fixtureInfo.FullName); assemblyTest.AddChild(CreateFixtureFromType(fixtureType, delegate(Test fixtureTest) { if (fixtureInfo.TestMethods == null) { return; } foreach (ITestMethodInfo testMethodInfo in fixtureInfo.TestMethods) { try { IMethodInfo methodType = fixtureType.GetMethod(testMethodInfo.Name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public); fixtureTest.AddChild(CreateTestFromMethod(methodType, null)); } catch (Exception ex) { TestModel.AddAnnotation(new Annotation(AnnotationType.Error, fixtureType, "An exception was thrown while exploring a csUnit test case.", ex)); } } })); } catch (Exception ex) { TestModel.AddAnnotation(new Annotation(AnnotationType.Error, assembly, "An exception was thrown while exploring a csUnit test fixture.", ex)); } } })); } }
public IMethodInfo GetMethod(string methodName, bool includePrivateMethod) { return(inner.GetMethod(methodName, includePrivateMethod)); }
public IMethodInfo GetMethod(string methodName, bool includePrivateMethod) { return(_typeInfoImplementation.GetMethod(methodName, includePrivateMethod)); }
private static void loadStatics() { lock (LOCK) { if (loadState == LoadState.Uninitialized) { loadState = LoadState.Loading; try { loggerType = Type.GetType("Amazon.Runtime.Internal.Util.Logger"); logMangerType = Type.GetType("log4net.Core.LoggerManager, log4net"); logMangerTypeInfo = TypeFactory.GetTypeInfo(logMangerType); if (logMangerType == null) { loadState = LoadState.Failed; } else { getLoggerWithTypeMethod = logMangerTypeInfo.GetMethod("GetLogger", new ITypeInfo[2] { TypeFactory.GetTypeInfo(typeof(Assembly)), TypeFactory.GetTypeInfo(typeof(Type)) }); logType = Type.GetType("log4net.Core.ILogger, log4net"); logTypeInfo = TypeFactory.GetTypeInfo(logType); levelType = Type.GetType("log4net.Core.Level, log4net"); levelTypeInfo = TypeFactory.GetTypeInfo(levelType); debugLevelPropertyValue = levelTypeInfo.GetField("Debug").GetValue(null); infoLevelPropertyValue = levelTypeInfo.GetField("Info").GetValue(null); errorLevelPropertyValue = levelTypeInfo.GetField("Error").GetValue(null); systemStringFormatType = Type.GetType("log4net.Util.SystemStringFormat, log4net"); logMethod = logTypeInfo.GetMethod("Log", new ITypeInfo[4] { TypeFactory.GetTypeInfo(typeof(Type)), levelTypeInfo, TypeFactory.GetTypeInfo(typeof(object)), TypeFactory.GetTypeInfo(typeof(Exception)) }); isEnabledForMethod = logTypeInfo.GetMethod("IsEnabledFor", new ITypeInfo[1] { levelTypeInfo }); if (getLoggerWithTypeMethod == null || isEnabledForMethod == null || logType == null || levelType == null || logMethod == null) { loadState = LoadState.Failed; } else { if (AWSConfigs.XmlSectionExists("log4net") && (AWSConfigs.LoggingConfig.LogTo & LoggingOptions.Log4Net) == LoggingOptions.Log4Net) { TypeFactory.GetTypeInfo(Type.GetType("log4net.Config.XmlConfigurator, log4net"))?.GetMethod("Configure", new ITypeInfo[0])?.Invoke(null, null); } loadState = LoadState.Success; } } } catch { loadState = LoadState.Failed; } } } }