本文整理汇总了C#中System.Reflection.MethodBase.Invoke方法的典型用法代码示例。如果您正苦于以下问题:C# MethodBase.Invoke方法的具体用法?C# MethodBase.Invoke怎么用?C# MethodBase.Invoke使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.MethodBase
的用法示例。
在下文中一共展示了MethodBase.Invoke方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunTest
private static TestResult RunTest(object instance, MethodBase method)
{
var result = new TestResult { Name = method.Name };
var sw = new Stopwatch();
sw.Start();
try
{
method.Invoke(instance, null);
result.WasSuccessful = true;
}
catch (Exception ex)
{
result.WasSuccessful = false;
result.Message = ex.ToString();
}
finally
{
sw.Stop();
result.Duration = sw.ElapsedMilliseconds;
}
return result;
}
示例2: Create
private ServiceEntry Create(string serviceId, MethodBase implementationMethod)
{
var type = implementationMethod.DeclaringType;
return new ServiceEntry
{
Descriptor = new ServiceDescriptor
{
Id = serviceId
},
Func = parameters =>
{
var instance = _serviceFactory.Create(type);
var list = new List<object>();
foreach (var parameterInfo in implementationMethod.GetParameters())
{
var value = parameters[parameterInfo.Name];
var parameterType = parameterInfo.ParameterType;
var parameter = _typeConvertibleService.Convert(value, parameterType);
list.Add(parameter);
}
var result = implementationMethod.Invoke(instance, list.ToArray());
return result;
}
};
}
示例3: ExecutePrivate
private void ExecutePrivate(MethodBase entryPoint, IEnumerable<string> additionalReferences)
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => CurrentDomainOnAssemblyResolve(args.Name, additionalReferences);
Environment.SetEnvironmentVariable("CSharpRunner_ArtifactsPath", artifactsPath);
entryPoint.Invoke(entryPoint.DeclaringType, null);
}
示例4: callAsync
public Task<object> callAsync(MethodBase method, object[] args)
{
Func<object> work = () =>
{
return method.Invoke(target, UnwrapArgs(method, args));
};
if (queue != null)
return Task.Factory.StartNew<object>(work, new System.Threading.CancellationToken(false), TaskCreationOptions.None, queue);
else
return Task.FromResult<object>(work.Invoke());
}
示例5: call
public object call(MethodBase method, object[] args)
{
Func<object> work = () =>
{
return method.Invoke(target, UnwrapArgs(method, args));
};
if (queue != null)
{
var t = Task.Factory.StartNew<object>(work, new System.Threading.CancellationToken(false), TaskCreationOptions.None, queue);
t.Wait();
return t.Result;
}
else
return work.Invoke();
}
示例6: FastCall
private static object FastCall(object o, MethodBase method, ParameterInfo[] Parameters, object[] args, Type objType, IReflect objIReflect)
{
int upperBound = args.GetUpperBound(0);
for (int i = 0; i <= upperBound; i++)
{
ParameterInfo info = Parameters[i];
object defaultValue = args[i];
if ((defaultValue is Missing) && info.IsOptional)
{
defaultValue = info.DefaultValue;
}
args[i] = ObjectType.CTypeHelper(defaultValue, info.ParameterType);
}
VBBinder.SecurityCheckForLateboundCalls(method, objType, objIReflect);
if (((objType != objIReflect) && !method.IsStatic) && !DoesTargetObjectMatch(o, method))
{
return InvokeMemberOnIReflect(objIReflect, method, BindingFlags.InvokeMethod, o, args);
}
VerifyObjRefPresentForInstanceCall(o, method);
return method.Invoke(o, args);
}
示例7: HandleVirtualMethods
private object HandleVirtualMethods(MethodBase method, object[] args) {
var methodName = method.Name;
// Handle properties if they are CMS properties
if (methodName.StartsWith("get_")) {
bool propertyExists;
var currentPage = (CmsPage)_target;
var propertyName = methodName.Substring(4);
var propertyData = currentPage.Property.GetPropertyValue(propertyName, out propertyExists);
if (propertyExists) {
return GetPropertyValue(method, propertyData);
}
}
// Handle everything else that isn't a CMS property
try {
return method.Invoke(_target, args);
}
catch (Exception exception) {
throw GetExceptionToRethrow(exception);
}
}
示例8: RunTest
public void RunTest(MethodBase method)
{
try
{
LogTestCaseStart(method.Name);
teststarted = true;
method.Invoke(this, null);
LogTestCasePassed(method.Name);
}
catch (TargetInvocationException aex)
{
Logger.RecordMessage(aex.InnerException.Message, MessageType.Exception, MessageSeverity.Error);
LogTestCaseFailed(method.Name);
throw aex.InnerException;
}
finally
{
teststarted = false;
}
}
示例9: InvokeAndUnwrapException
protected static object InvokeAndUnwrapException(MethodBase mb, object obj, object[] args)
{
#if FIRST_PASS
return null;
#else
try
{
return mb.Invoke(obj, args);
}
catch (TargetInvocationException x)
{
throw ikvm.runtime.Util.mapException(x.InnerException);
}
#endif
}
示例10: InvokeMethodWithoutTargetInvocationException
public static object InvokeMethodWithoutTargetInvocationException(MethodBase method, object obj, object[] args)
{
if (method == null)
throw new ArgumentNullException("method");
try
{
return method.Invoke(obj, args);
}
catch (TargetInvocationException ex)
{
RethrowWithNoStackTraceLoss(ex.InnerException);
throw;
}
}
示例11: CompileTimeValidate
///<summary>
/// Validate the aspect usage
///</summary>
///<param name="method">The method that the aspect is applied on</param>
///<returns>Returns true if all checks pass</returns>
public override bool CompileTimeValidate(MethodBase method)
{
if (method == null)
{
this.RaiseError(1, "The PostCompile aspect can only be applied on methods.", method.AsSignature());
return false;
}
if (!method.IsStatic)
{
this.RaiseError(2, "The PostCompile aspect can only be applied on static methods.", method.AsSignature());
return false;
}
if (method.GetParameters().Length > 0)
{
this.RaiseError(3, "The PostCompile aspect can only be applied on methods without arguments.", method.AsSignature());
return false;
}
if (method as MethodInfo == null)
{
this.RaiseError(4, "The PostCompile aspect can not be applied on constructor/deconstructors", method.AsSignature());
return false;
}
if (((MethodInfo)method).ReturnType != typeof(void))
{
this.RaiseError(5, "The PostCompile aspect can only be applied on methods returning nothing.", method.AsSignature());
return false;
}
this.Describe("On compilation, this method will be invoked", method);
try
{
method.Invoke(null, null);
this.Describe("Last post-compile run succeeded at {0}".F(DateTime.Now), method);
}
catch (TargetInvocationException ex) // when this exception is thrown, consider it a success, and optionally check if it has more descriptions to add
{
if (ex.InnerException is PostCompileSuccessException)
{
this.Describe("Last post-compile run succeeded at {0}".F(DateTime.Now), method);
foreach (var d in (ex.InnerException as PostCompileSuccessException).Descriptions)
{
this.Describe(d, method);
}
}
else
{
this.Describe("Last post-compile run failed at {0}".F(DateTime.Now), method);
this.Describe(ex.ToString(), method);
}
}
catch (Exception ex)
{
this.Describe("Last post-compile run failed at {0}".F(DateTime.Now), method);
this.Describe(ex.ToString(), method);
}
return true;
}
示例12: Intercept
/// <summary>
/// Intercepts the <see cref="MethodBase"/> in the proxy to return a replaced value.
/// </summary>
/// <param name="methodBase">
/// The <see cref="MethodBase"/> containing information about the current
/// invoked property.
/// </param>
/// <param name="value">
/// The object to set the <see cref="MethodBase"/> to if it is a setter.
/// </param>
/// <returns>
/// The <see cref="object"/> replacing the original implementation value.
/// </returns>
public object Intercept(MethodBase methodBase, object value)
{
const string Getter = "get_";
const string Setter = "set_";
var name = methodBase.Name;
var key = name.Substring(4);
var parameters = value == null ? new object[] { } : new[] { value };
// Attempt to get the value from the lazy members.
if (name.StartsWith(Getter))
{
if (this.lazyDictionary.ContainsKey(key))
{
return this.lazyDictionary[key].Value;
}
}
// Set the value, remove the old lazy value.
if (name.StartsWith(Setter))
{
if (this.lazyDictionary.ContainsKey(key))
{
this.lazyDictionary.Remove(key);
}
}
return methodBase.Invoke(this.target, parameters);
}
示例13: NonOwnerInvocationHandler
private object NonOwnerInvocationHandler(object target, MethodBase method, object[] parameters)
{
object result = null;
try
{
if(method.Name.Equals("set_HotOrNot"))
{
result = method.Invoke(target, parameters);
}
else
{
throw new UnauthorizedAccessException("You are not permitted to update another's personal information!");
}
}
catch(ApplicationException ex)
{
return ex.StackTrace;
}
return result;
}
示例14: InvokeSetupMethod
private void InvokeSetupMethod(MethodBase method)
{
if (method == null) return;
try
{
method.Invoke(_testInstance, new Object[0]);
}
catch
{
Debug.LogError(string.Format("[{2}] {0}.{1}", _testType, method.Name, "FAIL"));
}
}
示例15: Rewrite
public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
{
Type returnType;
if (callee.ReturnsSomething(out returnType))
throw new ArgumentException("The IgnoreOnDecompilation attribute may only be applied to methods returning a void result. Use StaticEvaluation instead.");
if (_call)
{
callee.Invoke(args.Select(a => a.Sample).ToArray());
}
return true;
}