请问高手 c# 如何动态创建方法

chenyuling 2009-09-18 03:22:11
比如我现在创建
void a1()
{
response.write("1");
}

void a2()
{
response.write("2");
}
…………
但是现在的情况是我要创建很多这样的方法(比如100个a1,a2这样的办法,有特殊需求)
总不能把他写100编遍吧,能不能c# 循环动态的创建方法。


...全文
237 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序漫步 2009-09-19
  • 打赏
  • 举报
回复
可以用反射。。


或者 继承啊
wjq 2009-09-19
  • 打赏
  • 举报
回复
描述下你的需求吧……
ICrazyBoy 2009-09-19
  • 打赏
  • 举报
回复
根据参数来考虑下子,写100个这样郁闷的方法,你会疯破......
把参数的规律考虑下子啊!
chen_ya_ping 2009-09-19
  • 打赏
  • 举报
回复
就算利用反射,写入了代码,那这些写入的代码也要编译啊,你怎么编译
chen_ya_ping 2009-09-19
  • 打赏
  • 举报
回复
其实我想这个肯定是你的需求没有搞清楚,要不这个就是你自己想出来的。
  • 打赏
  • 举报
回复
不知到楼主想干什么
  • 打赏
  • 举报
回复
楼主的设计模式应该功夫不深.
mngzilin 2009-09-19
  • 打赏
  • 举报
回复
4楼 5楼 9楼 12楼 的方法都可以一试
much0726 2009-09-18
  • 打赏
  • 举报
回复
编程思路错误。调用100次a吧,不同要求的用参数或重载
void a(string i)
{
response.write(i);
}
wuyq11 2009-09-18
  • 打赏
  • 举报
回复
写实体类和方法,属性,通过反射调用方法传递参数实现调用
Vincent.Q重剑无锋,大巧不工 电话:13867408830;QQ:154538878;邮箱:xiyang-0@163.com
反射使用方法
本文主要介绍net技术中"反射"的使用方法,包括如何对属性赋值,获取变量,属性,方法,事件的列表,如何设置属性及调用方法等.希望对新人有所帮助咯.

Class1 test = new Class1();
Type t = test.GetType();

FieldInfo[] test_field = t.GetFields();
foreach (FieldInfo item_field in test_field)
{
System.Console.WriteLine(item_field.Name);

}

PropertyInfo[] list_property = t.GetProperties();
foreach (PropertyInfo item_property in list_property)
{
System.Console.WriteLine(item_property.Name);
}

MethodInfo[] test_method = t.GetMethods();
foreach (MethodInfo item_method in test_method)
{
System.Console.WriteLine(item_method.Name);

}

EventInfo[] test_event = t.GetEvents();
foreach (EventInfo item_event in test_event)
{
System.Console.WriteLine(item_event.Name);

}

//赋值
t.GetProperty("ID").SetValue(test, 1, null);
t.GetProperty("Name").SetValue(test, "a", null);
//取值
int i= Convert.ToInt32(t.GetProperty("ID").GetValue(test, null));
string s= t.GetProperty("Name").GetValue(test, null).ToString();



string[] s = { "B" };
MethodInfo method_getauthorid = t.GetMethod("");

object obj_name = Activator.CreateInstance(t, s);
method_getauthorid.Invoke(obj_name, null);

object obj = Activator.CreateInstance(t);
method_getauthorid.Invoke(obj, null);
ruanwei1987 2009-09-18
  • 打赏
  • 举报
回复
创建那么多方法干什么?
写方法得找规律啊
BossFriday 2009-09-18
  • 打赏
  • 举报
回复
不过不推荐这样,如果可能,改变你的思路.
以上方法,一般应用于非常的特殊的情况下,比如直接把一段代码保存在数据库中,然后执行.
BossFriday 2009-09-18
  • 打赏
  • 举报
回复
看下我写的这段测试code(work a "string" function)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Text;
using Microsoft.CSharp;
using System.Reflection;
using System.CodeDom;
using System.CodeDom.Compiler;

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
objCompilerParameters.GenerateExecutable = false;
objCompilerParameters.GenerateInMemory = true;
CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, GenerateCode());

if (!cr.Errors.HasErrors)
{
Assembly objAssembly = cr.CompiledAssembly;
object objHelloWorld = objAssembly.CreateInstance("DynamicCodeGenerate.HelloWorld");

MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");
string strResult1 = (string)objMI.Invoke(objHelloWorld, null);

MethodInfo objMI2 = objHelloWorld.GetType().GetMethod("OutPut2");
string strReuslt2 = string.Empty;
object[] arguments = new object[1];
arguments[0] = "the input string";
strReuslt2 = (string)objMI2.Invoke(objHelloWorld, arguments);



Response.Write(strResult1 + "<br>" + strReuslt2);

}
else
{
foreach (CompilerError err in cr.Errors)
{
Response.Write(err.ErrorText + Environment.NewLine);
}

}
}

static string GenerateCode()
{
StringBuilder sb = new StringBuilder();
sb.Append("using System;");
sb.Append(Environment.NewLine);
sb.Append("namespace DynamicCodeGenerate");
sb.Append(Environment.NewLine);
sb.Append("{");
sb.Append(Environment.NewLine);
sb.Append(" public class HelloWorld");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);

sb.Append(" public string OutPut()");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" return \"Hello world!\";");
sb.Append(Environment.NewLine);
sb.Append(" }");

sb.Append(" public string OutPut2(string strInput)");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" return strInput + \" Hello world!\";");
sb.Append(Environment.NewLine);
sb.Append(" }");

sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append("}");

string code = sb.ToString();
return code;
}
}

}

haihuan23 2009-09-18
  • 打赏
  • 举报
回复
写个方法 传参数..
Lovely_baby 2009-09-18
  • 打赏
  • 举报
回复
也感觉你的要求是没有意义的
功能相仿的话 使用继承
风之影子 2009-09-18
  • 打赏
  • 举报
回复
说一下你的需求吧!
风之影子 2009-09-18
  • 打赏
  • 举报
回复
写一个方法
在里面进行条件判断.
基本的OOP思想.

tkscascor 2009-09-18
  • 打赏
  • 举报
回复
我觉得你这种想法是错误的...
说说你的具体需求怎么的. 为什么要动态生成那么多方法

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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