Tag: reflection

如何使用C#中的reflection查找实现generics抽象类的所有类?

我有一个看起来像这样的c#类 public abstract class Listener where T : Event { public abstract void Handle(T _event); } 我把这个类延伸到这个类 public class SendWelcomeEmail : Listener { public override void Handle(UserWasCreated _event) { //… } } 我需要使用reflection来查找扩展Listener基类的所有类。 我尝试了以下内容 var listeners = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(assembly => assembly.GetTypes()) .Where(x => x.IsClass && !x.IsInterface) .Where(listener => !listener.IsAbstract && listener.IsGenericType && listener.GetGenericTypeDefinition() == typeof(Listener)) […]

Moq和reflection,将动态生成的表达式树/ lambda传递给moq

是否可以编写如下代码。 我正在尝试将Moq与我正在反思的对象一起用作测试框架的一部分。 下面的代码从Moq引出了一个“Unhandled expression type:’Goto’”exception,我猜这个exception会有所不同。 它看起来应该工作了! private void button1_Click(object sender, EventArgs e) { Ifoo = foo Foo(); // Create input parameter for lambda ParameterExpression value = Expression.Parameter(typeof(IFoo), “value”); // create return statement for lambda Expression setupProperty = Expression.Return(Expression.Label(), Expression.Property(value, “Bar”), typeof(string)); // convert expression to lambda (should now be the equivalent of “v => v.Bar”) […]

使用C#中的reflection检测编译器生成的默认构造函数

我的目标是.NET 3.5 SP1,我正在使用CommentCheckervalidation我的XML文档,一切正常,直到我进入这样的类: /// /// documentation /// public sealed class MyClass { /// /// documentation /// public void Method() { } } 在上面的例子中,据我所知,编译器为我的类生成一个默认构造函数。 这样做的问题是CommentChecker会生成警告,告诉我构造函数缺少注释。 我试图修改程序以检测这种特殊情况并忽略它,但我卡住了,我已经尝试过IsDefined(typeof(CompilerGeneratedAttribute), true)但是没有用。 简而言之,我如何使用reflection检测默认构造函数?

System.Reflection.TargetInvocationException; 需要帮助理解

这个例外意味着什么,我该怎么做才能解决它?

从C#2.0中的dll获取Namespace,classname

我会动态地得到dll。 我需要加载dll并获取命名空间,classname来调用方法(方法名称是静态的,它将始终是“OnStart()”)。 基本上我需要通过加载dll来运行一个方法。 有人可以帮忙!!!

MEF,为什么创建同一个导出插件的相同副本?

(1)使用下面的代码我在我的容器中得到了两个相同的导出插件,我想知道为什么: (2)我真正无法实现的其他问题:如何扩展框架以处理不同的插件类型(例如,有多个不同类型的导入,或者一个导入将所有插件存储在动态IEnumerable中)。 我想在我的静态包装类中提供一个generics方法,该方法将发现的插件作为类型和匹配元数据的函数返回。 导出的插件(位于单独的dll中,其位置在构建DirectoryCatalog时指向。 [Export(typeof(IPlugin))] //<—- If this line is commented out then only one item is imported (why?) [PluginAttribute(typeof(StrategyPlugin_Test1), "StrategyPlugin", "Plugin1")] public class StrategyPlugin_Test1 : IPlugin { public void DoSomething() { Console.WriteLine("I do something"); } } 以下代码定义了强类型元数据和导入,以及执行MEF函数并应该保存已发现插件的静态类: [MetadataAttribute] [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class PluginAttribute : ExportAttribute { public Type PluginType { get; set; […]

如何在C#中编写一个“真正的”私有方法?

实际上,私有方法是在C#中实现的,仍然可以使用Reflection进行搜索。 我要做的是编写public string Encrypt(string data)和private string Decrypt(string cipher)方法来执行加密和解密。 不幸的是,如果有人知道.NET框架,他可以使用Reflection来查找Decrypt方法,并解密所有加密的方法。 似乎不那么安全。 所以我想把Decrypt方法变成真正的私有方法。 但是怎么做呢? 更新日期:2012年1月9日下午10:52悉尼时间 bdares提供了这个问题的技术解释 Eric Lippert提供了这个问题的政治解释 谢谢两位专家!

如何使用动态生成的对象作为CodeEffects生成器的数据源

我们正在使用这个组件www.codeeffects.com,它允许我们根据对象属性创建业务规则。 视图的html是这样的: @{ ViewBag.Title = “Post Example”; Html.CodeEffects().Styles() .SetTheme(ThemeType.Gray) .Render(); } @using (Html.BeginForm(“Evaluate”, “Post”, FormMethod.Post)) { Post Example Info: @ViewBag.Message @{ Html.CodeEffects().RuleEditor() .Id(“ruleEditor”) .SaveAction(“Save”, “Post”) .DeleteAction(“Delete”, “Post”) .LoadAction(“Load”, “Post”) .Mode(RuleType.Execution) .ContextMenuRules(ViewBag.ContextMenuRules) .ToolBarRules(ViewBag.ToolBarRules) .Rule(ViewBag.Rule) .Render(); } Rule Test Form @{ Html.RenderPartial(“_PatientForm”); } } @{ Html.CodeEffects().Scripts().Render(); } 控制器中的索引操作如下: [HttpGet] public ActionResult Index() { ViewBag.Rule = RuleModel.Create(typeof(Patient)); return […]

使用Reflection获取带参数的静态方法

我正在使用公共静态类和静态方法及其参数: public static class WLR3Logon { static void getLogon(int accountTypeID) {} } 现在我尝试将其参数的方法提取到另一个类并使用以下代码: MethodInfo inf = typeof(WLR3Logon).GetMethod(“getLogon”, BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy); int[] parameters = { accountTypeId }; foreach (int parameter in parameters) { inf.Invoke(“getLogon”, parameters); } 但它给了我错误 “你调用的对象是空的。” 哪里出错了

ProtoBuf-net AsReference需要Activator.CreateInstance中的公共构造函数吗?

在我的两个类中工作时看起来像这样(最小) using System; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.IO; using ProtoBuf; namespace Sandbox { public partial class Form1 : Form { public Form1() { Family family = new Family(); Child child1 = new Child(1); Child child2 = new Child(2); Parent parent = new Parent(new List() { child1, child2 }); […]