1. 用来调用指定类或对象的公有方法。
2. 几个比较重要的属性:
GenericTypeArguments:泛型参数集合
MethodName:调用的方法名称
Parameters:方法参数集合
Result:方法返回值
RunAsynchronously:是否以异步方式运行
TargetObject:调用方法的对象
TargetType:调用方法的类型
下面举几个典型的例子:
1.有参数有返回值
public class TestClass
{
public int TestdWithResult(int param1, int param2)
{
Console.WriteLine("Test With Result Value");
Console.WriteLine(string.Format("param1 (int): {0}", param1));
Console.WriteLine(string.Format("param2 (int): {0}", param2));
return param1 + param2;
}
}
2.泛型静态方法
public static void GenericStaticMethod<T1, T2>(T1 param1, T2 param2)
{
Console.WriteLine("Generic Static Method with Parameters");
Console.WriteLine(string.Format("typeof T1: {0}", typeof(T1)));
Console.WriteLine(string.Format("param1: {0}", param1));
Console.WriteLine(string.Format("typeof T2: {0}", typeof(T2)));
Console.WriteLine(string.Format("param2: {0}", param2));
}
2.1. 静态方法指定TargetType属性,实例方法指定TargetObject属性
2.2.指定GenericTypeArguments属性
3. 带Ref参数的方法
public void RefMethod(string param1, int param2, ref string param3)
{
Console.WriteLine("Ref Method with Parameters");
Console.WriteLine(string.Format("param1 (string): {0}", param1));
Console.WriteLine(string.Format("param2 (int): {0}", param2));
Console.WriteLine(string.Format("out param3 (string): {0}", param3));
param3 = "reference param changed!";
}
参数为in/out,绑定到变量:
4. 异步方法
// to be used for the async sample
AsyncCallback callback;
IAsyncResult asyncResult;
public void AsyncMethodSample(string message)
{
Console.WriteLine("....Called synchronous sample method with \"{0}\"", message);
Console.WriteLine("....This method will not be executed, BeginAsyncMethoSample / EndAsyncMethodSample will be executed instead");
}
// begin the async operation
public IAsyncResult BeginAsyncMethodSample(string message, AsyncCallback callback, object asyncState)
{
Console.WriteLine("....BeginAsyncMethodSample");
Console.WriteLine(string.Format("........Message: {0}", message));
this.callback = callback;
this.asyncResult = new TestAsyncResult() { AsyncState = asyncState };
Thread t = new Thread(new ThreadStart(ProcessThread));
t.Start();
return this.asyncResult;
}
// do some stuff and invoke the async callback
public void ProcessThread()
{
Console.Write("........Processing ");
for (int i = 0; i < 5; i++)
{
Console.Write(". ");
Thread.Sleep(500);
}
Console.WriteLine();
this.callback(this.asyncResult);
}
public void EndAsyncMethodSample(IAsyncResult r)
{
Console.WriteLine("....EndAsyncMethodSample");
}
class TestAsyncResult : IAsyncResult
{
public object AsyncState
{
get;
set;
}
public WaitHandle AsyncWaitHandle
{
get { throw new NotImplementedException(); }
}
public bool CompletedSynchronously
{
get { return true; }
}
public bool IsCompleted
{
get { return true; }
}
}
4.1.和其他不同的是我们要勾选RunAsynchronously属性
4.2.MethodName属性的 AsyncMethodSample方法不会调用,调用的是begin和end的方法。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?