新手求教,在webService中使用反射的问题。

吃啥补傻 2011-07-27 10:59:50
建立了一个新的webServices项目,在service.cs中有如下代码

[WebMethod]
public string hello() {
Assembly assembly = Assembly.Load("com.ms"); // 问题所在,该程序集该如何载入
//Assembly.Load("HelloWorld()").CreateInstance("com.ms.AbcTest()");
Type type = assembly.GetType("com.ehai.ms.AbcTest()");
Activator.CreateInstance(type);
MethodInfo mInfo = type.GetMethod("HelloWorld()");
return mInfo.Invoke(null,null).ToString();
//return "1";
}


在App_Code下有两个cs文件,其一是Service.cs, 另一是AbcTest.cs


namespace com.ms
{
public class AbcTest
{
public AbcTest()
{
//
// TODO: Add constructor logic here
//
}

public string HelloWorld()
{
return "Hello World";
}
}
}

那么此时该如何通过AbcTest.cs 中的namespace 使用反射调用到AbcTest中helloworld方法
...全文
82 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wddjzzxsdz 2011-07-27
  • 打赏
  • 举报
回复
那就没必要用反射吧。同一个程序集内还需要用反射来调用方法吗?直接用产生实例调用方法不就行了?
如果你真的想调用的话。网站在发布后会把App_Code里面的文件编译成App_Code.dll
吃啥补傻 2011-07-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fangxinggood 的回复:]

Assembly assembly = Assembly.Load("com.ms");

改为

Assembly assembly = Assembly.LoadFile(Server.MapPath("~/xxx.dll"));

用dll的绝对路径加载
[/Quote]
没有编译成dll哦,是在同一个solution下的app_code 文件夹下。 我想直接调用的这个.cs 类的方法
机器人 2011-07-27
  • 打赏
  • 举报
回复
Assembly assembly = Assembly.Load("com.ms");

改为

Assembly assembly = Assembly.LoadFile(Server.MapPath("~/xxx.dll"));

用dll的绝对路径加载

110,534

社区成员

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

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

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