C# 从未知类调用方法
可以从未知类调用方法吗C# 从未知类调用方法,c#,generics,C#,Generics,可以从未知类调用方法吗 public class ClientSampleResponse : IPacket { public string Name { get; set; } public string Lastname { get; set; } public string Address { get; set; } public void Execute<T>(T client) { var method = type
public class ClientSampleResponse : IPacket
{
public string Name { get; set; }
public string Lastname { get; set; }
public string Address { get; set; }
public void Execute<T>(T client)
{
var method = typeof(T).GetMethod("Send");
//method.Invoke(this);
}
}
公共类客户端采样器响应:IPacket
{
公共字符串名称{get;set;}
公共字符串Lastname{get;set;}
公共字符串地址{get;set;}
公共无效执行(T客户端)
{
var方法=typeof(T).GetMethod(“Send”);
//方法调用(this);
}
}
我正试图使用上述代码从未知类调用此方法:
public void Send<T>(T packet) where T : IPacket
{
// Skip contents
}
公共无效发送(T数据包),其中T:IPacket
{
//跳过内容
}
这应该可以:
public void Execute<T>(T client)
{
var method = typeof(T).GetMethod("Send");
method.Invoke(client, new object[] { this });
}
public void Execute(T客户端)
{
var方法=typeof(T).GetMethod(“Send”);
调用(客户端,新对象[]{this});
}
此外,请确保您的对象客户机不拥有多个发送方法,否则您应该考虑使用<代码> GetMethod(“发送”)、“新建类型[]{Type(IPHOPEN)})< /代码>。
这应该可以:public void Execute<T>(T client)
{
var method = typeof(T).GetMethod("Send");
method.Invoke(client, new object[] { this });
}
public void Execute(T客户端)
{
var方法=typeof(T).GetMethod(“Send”);
调用(客户端,新对象[]{this});
}
此外,请确保您的对象客户机不拥有多个发送方法,否则您应该考虑使用<代码> GetMethod(“发送”)、“新建类型[]{Type(IPHOPEN)})< /代码>。
您可以调用任何对象的任何方法,只要该方法存在 我发现代码示例非常清楚:
// Get the constructor and create an instance of MagicClass
Type magicType = Type.GetType("MagicClass");
ConstructorInfo magicConstructor = magicType.GetConstructor(Type.EmptyTypes);
object magicClassObject = magicConstructor.Invoke(new object[]{});
// Get the ItsMagic method and invoke with a parameter value of 100
MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});
我遗漏了什么吗?只要方法存在,您就可以调用任何对象的任何方法 我发现代码示例非常清楚:
// Get the constructor and create an instance of MagicClass
Type magicType = Type.GetType("MagicClass");
ConstructorInfo magicConstructor = magicType.GetConstructor(Type.EmptyTypes);
object magicClassObject = magicConstructor.Invoke(new object[]{});
// Get the ItsMagic method and invoke with a parameter value of 100
MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});
我遗漏了什么吗?您需要调用Invoke(客户端,this)而不是Invoke(this),因为这是ClientSamplerResponse,IPacket与T无关
public class ClientSampleResponse : IPacket
{
public string Name { get; set; }
public string Lastname { get; set; }
public string Address { get; set; }
public void Execute<T>(T client)
{
var method = typeof(T).GetMethod("Send");
method.Invoke(client, new object[] { this });
}
公共类客户端采样器响应:IPacket
{
公共字符串名称{get;set;}
公共字符串Lastname{get;set;}
公共字符串地址{get;set;}
公共无效执行(T客户端)
{
var方法=typeof(T).GetMethod(“Send”);
调用(客户端,新对象[]{this});
}
}
是的,参数以数组的形式发送您需要调用Invoke(客户端,this)而不是Invoke(this),因为这是clientSamplerResponse,IPacket与T无关
public class ClientSampleResponse : IPacket
{
public string Name { get; set; }
public string Lastname { get; set; }
public string Address { get; set; }
public void Execute<T>(T client)
{
var method = typeof(T).GetMethod("Send");
method.Invoke(client, new object[] { this });
}
公共类客户端采样器响应:IPacket
{
公共字符串名称{get;set;}
公共字符串Lastname{get;set;}
公共字符串地址{get;set;}
公共无效执行(T客户端)
{
var方法=typeof(T).GetMethod(“Send”);
调用(客户端,新对象[]{this});
}
}
是的,参数是以数组形式发送的,我能解释一下为什么它应该被否决?为什么它被降级?任何由谁提供的?注意:调用的正确语法是:
method.Invoke(instanceOfYourUnknownClass,parameter)
。如果Send不是静态方法,则Invoke
的第一个参数应该是对具有Send方法的类实例的引用。(我想客户机:method.Invoke(客户机,this);
如果它是静态方法,您可以通过传递null
作为第一个参数来调用它:method.Invoke(null,this);
see不能转换为对象[],这就是问题所在,您必须将它转换为新对象[]{this}我能解释一下为什么它应该被否决吗?为什么它被降级?任何人都可以解释吗?注意:调用的正确语法是:method.Invoke(instanceOfYourUnknownClass,parameter)
。如果Send不是静态方法,Invoke
的第一个参数应该是对带有Send方法的类实例的引用。(我想客户机:method.Invoke(客户机,this);
如果它是静态方法,您可以通过传递null
作为第一个参数来调用它:method.Invoke(null,this);
see不能转换为对象[],这就是问题所在,您必须将它转换为新对象[]{this}