How to use object method of Telerik.JustMock.Core.Castle.DynamicProxy.Generators.MethodFinder class

Best JustMockLite code snippet using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.MethodFinder.object

MembersCollector.cs

Source:MembersCollector.cs Github

copy

Full Screen

1// Copyright 2004-2011 Castle Project - http://www.castleproject.org/2// 3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6// 7// http://www.apache.org/licenses/LICENSE-2.08// 9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14namespace Telerik.JustMock.Core.Castle.DynamicProxy.Contributors15{16 using System;17 using System.Collections.Generic;18 using System.Linq;19 using System.Reflection;20 using Telerik.JustMock.Core.Castle.Core.Logging;21 using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;22 using Telerik.JustMock.Core.Castle.DynamicProxy.Internal;23 internal abstract class MembersCollector24 {25 private const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;26 private ILogger logger = NullLogger.Instance;27 private ICollection<MethodInfo> checkedMethods = new HashSet<MethodInfo>();28 private readonly IDictionary<PropertyInfo, MetaProperty> properties = new Dictionary<PropertyInfo, MetaProperty>();29 private readonly IDictionary<EventInfo, MetaEvent> events = new Dictionary<EventInfo, MetaEvent>();30 private readonly IDictionary<MethodInfo, MetaMethod> methods = new Dictionary<MethodInfo, MetaMethod>();31 protected readonly Type type;32 protected MembersCollector(Type type)33 {34 this.type = type;35 }36 public ILogger Logger37 {38 get { return logger; }39 set { logger = value; }40 }41 public IEnumerable<MetaMethod> Methods42 {43 get { return methods.Values; }44 }45 public IEnumerable<MetaProperty> Properties46 {47 get { return properties.Values; }48 }49 public IEnumerable<MetaEvent> Events50 {51 get { return events.Values; }52 }53 public virtual void CollectMembersToProxy(IProxyGenerationHook hook)54 {55 if (checkedMethods == null) // this method was already called!56 {57 throw new InvalidOperationException(58 string.Format("Can't call 'CollectMembersToProxy' method twice. This usually signifies a bug in custom {0}.",59 typeof(ITypeContributor)));60 }61 CollectProperties(hook);62 CollectEvents(hook);63 // Methods go last, because properties and events have methods too (getters/setters add/remove)64 // and we don't want to get duplicates, so we collect property and event methods first65 // then we collect methods, and add only these that aren't there yet66 CollectMethods(hook);67 checkedMethods = null; // this is ugly, should have a boolean flag for this or something68 }69 private void CollectProperties(IProxyGenerationHook hook)70 {71 var propertiesFound = type.GetProperties(Flags);72 foreach (var property in propertiesFound)73 {74 AddProperty(property, hook);75 }76 }77 private void CollectEvents(IProxyGenerationHook hook)78 {79 var eventsFound = type.GetEvents(Flags);80 foreach (var @event in eventsFound)81 {82 AddEvent(@event, hook);83 }84 }85 private void CollectMethods(IProxyGenerationHook hook)86 {87 var methodsFound = MethodFinder.GetAllInstanceMethods(type, Flags);88 foreach (var method in methodsFound)89 {90 AddMethod(method, hook, true);91 }92 }93 private void AddProperty(PropertyInfo property, IProxyGenerationHook hook)94 {95 MetaMethod getter = null;96 MetaMethod setter = null;97 if (property.CanRead)98 {99 var getMethod = property.GetGetMethod(true);100 getter = AddMethod(getMethod, hook, false);101 }102 if (property.CanWrite)103 {104 var setMethod = property.GetSetMethod(true);105 setter = AddMethod(setMethod, hook, false);106 }107 if (setter == null && getter == null)108 {109 return;110 }111 var nonInheritableAttributes = property.GetNonInheritableAttributes();112 var arguments = property.GetIndexParameters();113 properties[property] = new MetaProperty(property.Name,114 property.PropertyType,115 property.DeclaringType,116 getter,117 setter,118 nonInheritableAttributes.Select(a => a.Builder),119 arguments.Select(a => a.ParameterType).ToArray());120 }121 private void AddEvent(EventInfo @event, IProxyGenerationHook hook)122 {123 var addMethod = @event.GetAddMethod(true);124 var removeMethod = @event.GetRemoveMethod(true);125 MetaMethod adder = null;126 MetaMethod remover = null;127 if (addMethod != null)128 {129 adder = AddMethod(addMethod, hook, false);130 }131 if (removeMethod != null)132 {133 remover = AddMethod(removeMethod, hook, false);134 }135 if (adder == null && remover == null)136 {137 return;138 }139 events[@event] = new MetaEvent(@event.Name,140 @event.DeclaringType, @event.EventHandlerType, adder, remover, EventAttributes.None);141 }142 private MetaMethod AddMethod(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)143 {144 if (checkedMethods.Contains(method))145 {146 return null;147 }148 checkedMethods.Add(method);149 if (methods.ContainsKey(method))150 {151 return null;152 }153 var methodToGenerate = GetMethodToGenerate(method, hook, isStandalone);154 if (methodToGenerate != null)155 {156 methods[method] = methodToGenerate;157 }158 return methodToGenerate;159 }160 protected abstract MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone);161 /// <summary>162 /// Performs some basic screening and invokes the <see cref = "IProxyGenerationHook" />163 /// to select methods.164 /// </summary>165 /// <param name = "method"></param>166 /// <param name = "onlyVirtuals"></param>167 /// <param name = "hook"></param>168 /// <returns></returns>169 protected bool AcceptMethod(MethodInfo method, bool onlyVirtuals, IProxyGenerationHook hook)170 {171 if (IsInternalAndNotVisibleToDynamicProxy(method))172 {173 return false;174 }175 var isOverridable = method.IsVirtual && !method.IsFinal;176 if (onlyVirtuals && !isOverridable)177 {178 if (179#if FEATURE_REMOTING180 method.DeclaringType != typeof(MarshalByRefObject) &&181#endif182 method.IsGetType() == false &&183 method.IsMemberwiseClone() == false)184 {185 Logger.DebugFormat("Excluded non-overridable method {0} on {1} because it cannot be intercepted.", method.Name,186 method.DeclaringType.FullName);187 hook.NonProxyableMemberNotification(type, method);188 }189 return false;190 }191 // we can never intercept a sealed (final) method192 if (method.IsFinal)193 {194 Logger.DebugFormat("Excluded sealed method {0} on {1} because it cannot be intercepted.", method.Name,195 method.DeclaringType.FullName);196 return false;197 }198 //can only proxy methods that are public or protected (or internals that have already been checked above)199 if ((method.IsPublic || method.IsFamily || method.IsAssembly || method.IsFamilyOrAssembly) == false)200 {201 return false;202 }203#if FEATURE_REMOTING204 if (method.DeclaringType == typeof(MarshalByRefObject))205 {206 return false;207 }208#endif209 if (method.IsFinalizer())210 {211 return false;212 }213 return hook.ShouldInterceptMethod(type, method);214 }215 private static bool IsInternalAndNotVisibleToDynamicProxy(MethodInfo method)216 {217 return ProxyUtil.IsInternal(method) &&218 ProxyUtil.AreInternalsVisibleToDynamicProxy(method.DeclaringType.GetTypeInfo().Assembly) == false;219 }220 }221}...

Full Screen

Full Screen

InvocationHelper.cs

Source:InvocationHelper.cs Github

copy

Full Screen

...27 {28 private static readonly Dictionary<CacheKey, MethodInfo> cache =29 new Dictionary<CacheKey, MethodInfo>();30 private static readonly Lock @lock = Lock.Create();31 public static MethodInfo GetMethodOnObject(object target, MethodInfo proxiedMethod)32 {33 if (target == null)34 {35 return null;36 }37 return GetMethodOnType(target.GetType(), proxiedMethod);38 }39 public static MethodInfo GetMethodOnType(Type type, MethodInfo proxiedMethod)40 {41 if (type == null)42 {43 throw new ArgumentNullException("type");44 }45 Debug.Assert(proxiedMethod.DeclaringType.IsAssignableFrom(type),46 "proxiedMethod.DeclaringType.IsAssignableFrom(type)");47 using (var locker = @lock.ForReading())48 {49 var methodOnTarget = GetFromCache(proxiedMethod, type);50 if (methodOnTarget != null)51 {52 return methodOnTarget;53 }54 }55 using (var locker = @lock.ForReadingUpgradeable())56 {57 var methodOnTarget = GetFromCache(proxiedMethod, type);58 if (methodOnTarget != null)59 {60 return methodOnTarget;61 }62 // Upgrade the lock to a write lock. 63 using (locker.Upgrade())64 {65 methodOnTarget = ObtainMethod(proxiedMethod, type);66 PutToCache(proxiedMethod, type, methodOnTarget);67 }68 return methodOnTarget;69 }70 }71 private static MethodInfo GetFromCache(MethodInfo methodInfo, Type type)72 {73 var key = new CacheKey(methodInfo, type);74 MethodInfo method;75 cache.TryGetValue(key, out method);76 return method;77 }78 private static MethodInfo ObtainMethod(MethodInfo proxiedMethod, Type type)79 {80 Type[] genericArguments = null;81 if (proxiedMethod.IsGenericMethod)82 {83 genericArguments = proxiedMethod.GetGenericArguments();84 proxiedMethod = proxiedMethod.GetGenericMethodDefinition();85 }86 var declaringType = proxiedMethod.DeclaringType;87 MethodInfo methodOnTarget = null;88 if (declaringType.GetTypeInfo().IsInterface)89 {90 var mapping = type.GetTypeInfo().GetRuntimeInterfaceMap(declaringType);91 var index = Array.IndexOf(mapping.InterfaceMethods, proxiedMethod);92 Debug.Assert(index != -1);93 methodOnTarget = mapping.TargetMethods[index];94 }95 else96 {97 // NOTE: this implementation sucks, feel free to improve it.98 var methods = MethodFinder.GetAllInstanceMethods(type, BindingFlags.Public | BindingFlags.NonPublic);99 foreach (var method in methods)100 {101 if (MethodSignatureComparer.Instance.Equals(method.GetBaseDefinition(), proxiedMethod))102 {103 methodOnTarget = method;104 break;105 }106 }107 }108 if (methodOnTarget == null)109 {110 throw new ArgumentException(111 string.Format("Could not find method overriding {0} on type {1}. This is most likely a bug. Please report it.",112 proxiedMethod, type));113 }114 if (genericArguments == null)115 {116 return methodOnTarget;117 }118 return methodOnTarget.MakeGenericMethod(genericArguments);119 }120 private static void PutToCache(MethodInfo methodInfo, Type type, MethodInfo value)121 {122 var key = new CacheKey(methodInfo, type);123 cache.Add(key, value);124 }125 private struct CacheKey : IEquatable<CacheKey>126 {127 public CacheKey(MethodInfo method, Type type)128 {129 Method = method;130 Type = type;131 }132 public MethodInfo Method { get; }133 public Type Type { get; }134 public bool Equals(CacheKey other)135 {136 return object.ReferenceEquals(Method, other.Method) && object.ReferenceEquals(Type, other.Type);137 }138 public override bool Equals(object obj)139 {140 if (ReferenceEquals(null, obj))141 return false;142 return obj is CacheKey @struct && Equals(@struct);143 }144 public override int GetHashCode()145 {146 unchecked147 {148 return ((Method != null ? Method.GetHashCode() : 0) * 397) ^ (Type != null ? Type.GetHashCode() : 0);149 }150 }151 }152 }...

Full Screen

Full Screen

MethodFinder.cs

Source:MethodFinder.cs Github

copy

Full Screen

...23 /// </summary>24 internal class MethodFinder25 {26 private static readonly Dictionary<Type, MethodInfo[]> cachedMethodInfosByType = new Dictionary<Type, MethodInfo[]>();27 private static readonly object lockObject = new object();28 public static MethodInfo[] GetAllInstanceMethods(Type type, BindingFlags flags)29 {30 if ((flags & ~(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) != 0)31 {32 throw new ArgumentException("MethodFinder only supports the Public, NonPublic, and Instance binding flags.", "flags");33 }34 MethodInfo[] methodsInCache;35 lock (lockObject)36 {37 if (!cachedMethodInfosByType.TryGetValue(type, out methodsInCache))38 {39 // We always load all instance methods into the cache, we will filter them later40 methodsInCache = type.GetMethods(41 BindingFlags.Public | BindingFlags.NonPublic...

Full Screen

Full Screen

object

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Reflection;6using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;7{8 {9 static void Main(string[] args)10 {11 MethodInfo method = typeof(Program).GetMethod("Main", BindingFlags.Public | BindingFlags.Static);12 MethodFinder methodFinder = new MethodFinder();13 MethodInfo[] methods = methodFinder.FindAll(method);14 foreach (MethodInfo m in methods)15 {16 Console.WriteLine(m.Name);17 }18 Console.ReadLine();19 }20 }21}

Full Screen

Full Screen

object

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;6using Telerik.JustMock.Core;7{8 {9 static void Main(string[] args)10 {11 var methodFinder = new MethodFinder();12 var method = methodFinder.FindMethod(typeof(TestClass), "TestMethod");13 Console.WriteLine("Method name: " + method.Name);14 Console.ReadLine();15 }16 }17 {18 public void TestMethod()19 {20 }21 }22}

Full Screen

Full Screen

object

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;7{8 {9 public void Test()10 {11 var method = MethodFinder.FindMethod(typeof(Class1), "Test");12 }13 }14}15using System;16using System.Collections.Generic;17using System.Linq;18using System.Text;19using System.Threading.Tasks;20using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;21{22 {23 public void Test()24 {25 var method = MethodFinder.FindMethod(typeof(Class2), "Test");26 }27 }28}29using System;30using System.Collections.Generic;31using System.Linq;32using System.Text;33using System.Threading.Tasks;34using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;35{36 {37 public void Test()38 {39 var method = MethodFinder.FindMethod(typeof(Class3), "Test");40 }41 }42}43using System;44using System.Collections.Generic;45using System.Linq;46using System.Text;47using System.Threading.Tasks;48using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;49{50 {51 public void Test()52 {53 var method = MethodFinder.FindMethod(typeof(Class4), "Test");54 }55 }56}57using System;58using System.Collections.Generic;59using System.Linq;60using System.Text;61using System.Threading.Tasks;62using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;63{64 {65 public void Test()66 {

Full Screen

Full Screen

object

Using AI Code Generation

copy

Full Screen

1using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;2using System;3using System.Reflection;4using System.Collections.Generic;5using System.Linq;6using System.Text;7using System.Threading.Tasks;8{9 {10 static void Main(string[] args)11 {12 var method = MethodFinder.FindMethod(typeof(Interface1).GetMethod("Method1"), typeof(Class1));13 Console.WriteLine(method);14 Console.ReadLine();15 }16 }17 {18 void Method1();19 }20 {21 public void Method1()22 {23 }24 }25}26using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;27using System;28using System.Reflection;29using System.Collections.Generic;30using System.Linq;31using System.Text;32using System.Threading.Tasks;33{34 {35 static void Main(string[] args)36 {37 var method = MethodGenerator.GetMethod(typeof(Interface1).GetMethod("Method1"), typeof(Class1));38 Console.WriteLine(method);39 Console.ReadLine();40 }41 }42 {43 void Method1();44 }45 {46 public void Method1()47 {48 }49 }50}51using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;52using System;53using System.Reflection;54using System.Collections.Generic;55using System.Linq;56using System.Text;57using System.Threading.Tasks;58{59 {60 static void Main(string[] args)61 {62 var method = MethodGenerator.GetMethod(typeof(Interface1).GetMethod("Method1"), typeof(Class1));63 Console.WriteLine(method);64 Console.ReadLine();65 }66 }67 {68 void Method1();69 }70 {71 public void Method1()72 {73 }74 }75}

Full Screen

Full Screen

object

Using AI Code Generation

copy

Full Screen

1using System;2using System.Linq;3using System.Reflection;4using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;5{6 public static void Main()7 {8 var method = MethodFinder.FindMethods(typeof(Example), "ExampleMethod").Single();9 var parameters = method.GetParameters();10 var parameter = parameters[0];11 var parameterType = parameter.ParameterType;12 Console.WriteLine(parameterType);13 }14}15using System;16using System.Linq;17using System.Reflection;18{19 public static void Main()20 {21 var method = typeof(Example).GetMethod("ExampleMethod");22 var parameters = method.GetParameters();23 var parameter = parameters[0];24 var parameterType = parameter.ParameterType;25 Console.WriteLine(parameterType);26 }27}28using System;29using System.Linq;30using System.Reflection;31{32 public static void Main()33 {34 var method = typeof(Example).GetMethod("ExampleMethod");35 var parameters = method.GetParameters();36 var parameter = parameters[0];37 var parameterType = parameter.ParameterType;38 Console.WriteLine(parameterType);39 }40}41using System;42using System.Linq;43using System.Reflection;44{45 public static void Main()46 {47 var method = typeof(Example).GetMethod("ExampleMethod");48 var parameters = method.GetParameters();49 var parameter = parameters[0];50 var parameterType = parameter.ParameterType;51 Console.WriteLine(parameterType);52 }53}54using System;55using System.Linq;56using System.Reflection;57{58 public static void Main()59 {60 var method = typeof(Example).GetMethod("ExampleMethod");61 var parameters = method.GetParameters();62 var parameter = parameters[0];63 var parameterType = parameter.ParameterType;64 Console.WriteLine(parameterType);65 }66}67using System;68using System.Linq;69using System.Reflection;70{71 public static void Main()72 {73 var method = typeof(

Full Screen

Full Screen

object

Using AI Code Generation

copy

Full Screen

1using System;2using System.Reflection;3using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;4{5 {6 static void Main(string[] args)7 {8 var method = MethodFinder.Instance.GetMethod(typeof(Example), "DoSomething", new Type[] { typeof(int) });9 Console.WriteLine(method.Name);10 Console.ReadLine();11 }12 }13 {14 public void DoSomething(int a)15 {16 }17 }18}19I have tried this solution and it works. However, I have a follow up question. Is it possible to get the method name from a MethodInfo object? I have tried to get the method name from the MethodInfo object but I get the following error: "System.MissingMethodException: Method not found: 'System.String System.Reflection.MethodInfo.get_Name()'. "20MethodInfo method = MethodFinder.Instance.GetMethod(typeof(Example), "DoSomething", new Type[] { typeof(int) });21string methodName = method.Name;

Full Screen

Full Screen

object

Using AI Code Generation

copy

Full Screen

1{2 using System;3 using System.Reflection;4 using System.Reflection.Emit;5 {6 public static MethodInfo FindMethod(Type type, string name, Type[] argumentTypes)7 {8 return FindMethod(type, name, argumentTypes, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);9 }10 public static MethodInfo FindMethod(Type type, string name, Type[] argumentTypes, BindingFlags bindingFlags)11 {12 MethodBase[] methods = type.GetMethods(bindingFlags);13 foreach (MethodBase method in methods)14 {15 if (method.Name != name)16 {17 continue;18 }19 ParameterInfo[] methodParams = method.GetParameters();20 if (methodParams.Length != argumentTypes.Length)21 {22 continue;23 }24 bool found = true;25 for (int i = 0; i < methodParams.Length; i++)26 {27 if (methodParams[i].ParameterType != argumentTypes[i])28 {29 found = false;30 break;31 }32 }33 if (found)34 {35 return (MethodInfo)method;36 }37 }38 return null;39 }40 public static MethodInfo FindMethod(Type type, string name, Type[] argumentTypes, Type[] genericArguments)41 {42 MethodInfo method = FindMethod(type, name, argumentTypes, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);43 if (method != null && method.IsGenericMethodDefinition)44 {45 method = method.MakeGenericMethod(genericArguments);46 }47 return method;48 }49 }50}51{52 using System;53 using System.Reflection;54 using System.Reflection.Emit;55 {56 public static MethodInfo FindMethod(Type type, string name, Type[] argumentTypes)57 {58 return FindMethod(type, name, argumentTypes, BindingFlags.Public |

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run JustMockLite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in MethodFinder

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful