public void MesureTest() { const string category = "ProfilerTest.MeasureTest"; Profiler.Enabled = true; Profiler.AutoSaveResults = false; const int iteration = 10000; using (Profiler.Measure(category, "FullLength")) { MethodAccessor mi = MethodAccessor.GetAccessor(((Func <int>)TestMethod).Method); for (int i = 0; i < iteration; i++) { using (Profiler.Measure(category, "InvokeMethod")) { mi.Invoke(this); } } } Assert.AreEqual(2, Profiler.GetMeasurementResults(category).Count()); var full = Profiler.GetMeasurementResult(category, "FullLength"); var invoke = Profiler.GetMeasurementResult(category, "InvokeMethod"); Assert.AreEqual(1, full.NumberOfCalls); Assert.AreEqual(iteration, invoke.NumberOfCalls); Assert.IsTrue(full.TotalTime >= invoke.TotalTime); }
public void MethodAccessorInvoker_Test() { var methodInfo = typeof(MethodAccessorTest).GetTypeInfo().GetMethod("TestMethod"); var methodAccessor = new MethodAccessor(methodInfo); var methodInvoker = methodAccessor.CreateMethodInvoker(); Assert.Equal(methodInvoker.Invoke(this, EmptyArray <object> .Value), TestMethod()); }
public void 執行Sum方法() { var instance = new MyClass(); var methodInfo = instance.GetType().GetMethod("Sum"); var accessor = new MethodAccessor(methodInfo); var result = accessor.Execute(instance, 1, 1); Assert.AreEqual(2, result); }
public override void OnLoad() { var uconomyPlugin = R.Plugins.GetPlugins().FirstOrDefault(c => c.Name.EqualsIgnoreCase("uconomy")); var uconomyType = uconomyPlugin.GetType().Assembly.GetType("fr34kyn01535.Uconomy.Uconomy"); var uconomyInstance = uconomyType.GetField("Instance", BindingFlags.Static | BindingFlags.Public).GetValue(uconomyPlugin); var databaseInstance = uconomyInstance.GetType().GetField("Database").GetValue(uconomyInstance); _getBalanceMethod = AccessorFactory.AccessMethod <decimal>(databaseInstance, "GetBalance"); _increaseBalanceMethod = AccessorFactory.AccessMethod <decimal>(databaseInstance, "IncreaseBalance"); }
private static IMethodAccessor ExtractMethodInfo(Expression <Func <AuditPropertyContext, object> > methodExpression) { var memberExpression = methodExpression.Body as MethodCallExpression; if (memberExpression == null) { return(null); } var methodInfo = memberExpression.Method; var methodAccess = new MethodAccessor(methodInfo); return(methodAccess); }
private void InitializeAccessors() { var gridViewAccessor = new FieldAccessor(this, "gridView"); reflectedGridView = gridViewAccessor.Get(); var gridViewType = reflectedGridView.GetType(); allGridEntriesAccessor = new FieldAccessor(reflectedGridView, "allGridEntries"); topLevelGridEntriesAccessor = new FieldAccessor(reflectedGridView, "topLevelGridEntries"); totalPropsAccessor = new FieldAccessor(reflectedGridView, "totalProps"); selectedRowAccessor = new FieldAccessor(reflectedGridView, "selectedRow"); setScrollOffsetAccessor = new MethodAccessor(gridViewType, "SetScrollOffset"); refreshAccessor = new MethodAccessor(gridViewType, "Refresh"); selectedGridEntryAccessor = new PropertyAccessor(reflectedGridView, "SelectedGridEntry"); }
/// <summary> /// Invokes the underlying method represented by this {@code Method} /// object, on the specified object with the specified parameters. /// Individual parameters are automatically unwrapped to match /// primitive formal parameters, and both primitive and reference /// parameters are subject to method invocation conversions as /// necessary. /// /// <para>If the underlying method is static, then the specified {@code obj} /// argument is ignored. It may be null. /// /// </para> /// <para>If the number of formal parameters required by the underlying method is /// 0, the supplied {@code args} array may be of length 0 or null. /// /// </para> /// <para>If the underlying method is an instance method, it is invoked /// using dynamic method lookup as documented in The Java Language /// Specification, Second Edition, section 15.12.4.4; in particular, /// overriding based on the runtime type of the target object will occur. /// /// </para> /// <para>If the underlying method is static, the class that declared /// the method is initialized if it has not already been initialized. /// /// </para> /// <para>If the method completes normally, the value it returns is /// returned to the caller of invoke; if the value has a primitive /// type, it is first appropriately wrapped in an object. However, /// if the value has the type of an array of a primitive type, the /// elements of the array are <i>not</i> wrapped in objects; in /// other words, an array of primitive type is returned. If the /// underlying method return type is void, the invocation returns /// null. /// /// </para> /// </summary> /// <param name="obj"> the object the underlying method is invoked from </param> /// <param name="args"> the arguments used for the method call </param> /// <returns> the result of dispatching the method represented by /// this object on {@code obj} with parameters /// {@code args} /// </returns> /// <exception cref="IllegalAccessException"> if this {@code Method} object /// is enforcing Java language access control and the underlying /// method is inaccessible. </exception> /// <exception cref="IllegalArgumentException"> if the method is an /// instance method and the specified object argument /// is not an instance of the class or interface /// declaring the underlying method (or of a subclass /// or implementor thereof); if the number of actual /// and formal parameters differ; if an unwrapping /// conversion for primitive arguments fails; or if, /// after possible unwrapping, a parameter value /// cannot be converted to the corresponding formal /// parameter type by a method invocation conversion. </exception> /// <exception cref="InvocationTargetException"> if the underlying method /// throws an exception. </exception> /// <exception cref="NullPointerException"> if the specified object is null /// and the method is an instance method. </exception> /// <exception cref="ExceptionInInitializerError"> if the initialization /// provoked by this method fails. </exception> //JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @CallerSensitive public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: public Object Invoke(Object obj, params Object[] args) { if (!@override) { if (!Reflection.quickCheckMemberAccess(Clazz, Modifiers_Renamed)) { Class caller = Reflection.CallerClass; CheckAccess(caller, Clazz, obj, Modifiers_Renamed); } } MethodAccessor ma = MethodAccessor_Renamed; // read volatile if (ma == AnnotatedElement_Fields.Null) { ma = AcquireMethodAccessor(); } return(ma.invoke(obj, args)); }
internal MethodAccessor GetAddMethod(DataTypeDescriptor descriptor) { MethodAccessor GetAddMethodAccessor(Type type) { string methodName = SpecificAddMethod ?? "Add"; // if not specified called for .NET 3.5 with null dictionary values. MethodInfo method = IsGeneric ? type.GetMethod(methodName, type.GetGenericArguments()) // Using type arguments to eliminate ambiguity (LinkedList<T>.AddLast) : type.GetMethod(methodName); // For non-generics arguments are not always objects (StringDictionary) return(MethodAccessor.GetAccessor(method)); } if (addMethodCache == null) { Interlocked.CompareExchange(ref addMethodCache, new Cache <Type, MethodAccessor>(GetAddMethodAccessor).GetThreadSafeAccessor(), null); } return(addMethodCache[descriptor.Type]); }
/// <summary> /// Gets the accessor for a member /// The accessor is created if it does not exist /// </summary> /// <param name="info"></param> /// <returns>An accessor to access the given member with. Never null</returns> public IMemberAccessor GetAccessor(MemberInfo info) { if (info == null) { throw new ArgumentNullException(nameof(info)); } if (Members.TryGetValue(info, out var accessor)) { return(accessor); } //See if it's part of this type or a parent if (Type.Equals(info.DeclaringType)) { if (info is PropertyInfo prop) { accessor = new PropertyAccessor(prop); } else if (info is FieldInfo field) { accessor = new FieldAccessor(field); } else if (info is MethodInfo method) { accessor = new MethodAccessor(method); } //Cache and return it if (accessor != null) { Members.Add(info, accessor); } return(accessor); } //If this fails then the given member was not a member of this type's class hierarchy return(Parent?.GetAccessor(info) ?? throw new InvalidOperationException("Could not create accessor for member")); }
// NOTE that there is no synchronization used here. It is correct // (though not efficient) to generate more than one MethodAccessor // for a given Method. However, avoiding synchronization will // probably make the implementation more scalable. private MethodAccessor AcquireMethodAccessor() { // First check to see if one has been created yet, and take it // if so MethodAccessor tmp = AnnotatedElement_Fields.Null; if (Root_Renamed != AnnotatedElement_Fields.Null) { tmp = Root_Renamed.MethodAccessor; } if (tmp != AnnotatedElement_Fields.Null) { MethodAccessor_Renamed = tmp; } else { // Otherwise fabricate one and propagate it up to the root tmp = ReflectionFactory.newMethodAccessor(this); MethodAccessor = tmp; } return(tmp); }