如何利用传入的Type类型来调用范型方法

nuaalfm 2008-08-29 06:17:02
我希望使用的是从外边传过来的类型tt来调用test1范型方法
class Program
{
static void Main(string[] args)
{
MyClass m = new MyClass();
m.tt = typeof(Program);
m.test2();
}


}
[Serializable]
class MyClass
{
public Type tt { get; set; }
public int userid { get; set; }
public string Name { get; set; }
public string test2()
{
// test1<T>();
我希望使用的是从外边传过来的类型tt来调用test1范型方法
}
public string test1<T>()
{
return typeof(T).ToString();
}

}
...全文
212 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
vwxyzh 2008-09-02
  • 打赏
  • 举报
回复

MyClass instance = ...
Type genericParameter = ...
object[] parameters = ...
object result = typeof(MyClass).GetMethod("test1").MakeGenericMethod(genericParameter).Invoke(instance, parameters);
nuaalfm 2008-09-02
  • 打赏
  • 举报
回复
大家再研究研究
wnboy2002 2008-09-02
  • 打赏
  • 举报
回复
好,顶
JaggerLee 2008-09-01
  • 打赏
  • 举报
回复
重载一个
nuaalfm 2008-09-01
  • 打赏
  • 举报
回复
没人会么?
GhostAdai 2008-09-01
  • 打赏
  • 举报
回复
接分,哦耶!
nuaalfm 2008-09-01
  • 打赏
  • 举报
回复
这个反射该怎么写,也就是说根据已经配置好的类型信息来调用范型方法
nuaalfm 2008-09-01
  • 打赏
  • 举报
回复
其实我就是想将某个T传进类中存起来,然后使用它来调用其它的范型方法
真相重于对错 2008-08-29
  • 打赏
  • 举报
回复
反射
nuaalfm 2008-08-29
  • 打赏
  • 举报
回复
主要是test1这个方法是写好的格式就格式就是这个样子了,不能改成楼上的那种
wuyi8808 2008-08-29
  • 打赏
  • 举报
回复
    using System;

class Program
{
static void Main(string[] args)
{
MyClass m = new MyClass();
m.tt = typeof(Program);
string s = m.test2();
Console.WriteLine(s);
}
}

[Serializable]
class MyClass
{
public Type tt;

public string test2()
{
return test1(tt);
}

public string test1 <T>(T x)
{
return x.ToString();
}
}
wuyi8808 2008-08-29
  • 打赏
  • 举报
回复
    using System;

class Program
{
static void Main(string[] args)
{
MyClass m = new MyClass();
m.tt = typeof(Program);
string s = m.test2();
Console.WriteLine(s);
}


}
[Serializable]
class MyClass
{
public Type tt;

public string test2()
{
return test1(tt);
}

public string test1 <T>(T x)
{
return x.GetType().ToString();
}

}

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧