Tag: reflection

Reflection MethodInfo.Invoke()从方法内部捕获exception

我调用了MethodInfo.Invoke()来通过reflection执行一个函数。 调用包装在try/catch块中,但它仍然不会捕获我正在调用的函数抛出的exception。 我收到以下消息: 用户未处理exception。 为什么MethodInfo.Invoke()阻止在Invoke()之外捕获exception? 我该如何绕过它?

在运行时解析参数名称

可能重复: 查找传递给C#中函数的变量名称 在C#中,有没有办法(更好的方法)在运行时解析参数的名称? 例如,在以下方法中,如果重命名方法参数,则还必须记住更新传递给ArgumentNullException的字符串文字。 public void Woof(object resource) { if (resource == null) { throw new ArgumentNullException(“resource”); } // .. }

如何编写在C#中实现给定接口的通用容器类?

上下文:.NET 3.5,VS2008。 我不确定这个问题的标题,所以也可以自由评论标题:-) 这是场景:我有几个类,比如Foo和Bar,它们都实现了以下接口: public interface IStartable { void Start(); void Stop(); } 现在我想要一个容器类,它在构造函数中获取一个IEnumerable 作为参数。 反过来,这个类也应该实现IStartable接口: public class StartableGroup : IStartable // this is the container class { private readonly IEnumerable startables; public StartableGroup(IEnumerable startables) { this.startables = startables; } public void Start() { foreach (var startable in startables) { startable.Start(); } } public void […]

使用Reflection从类创建DataTable?

我刚刚学习了generics,我想知道我是否可以用它来动态地从我的类中构建数据表。 或者我可能在这里忽略了这一点。 这是我的代码,我要做的是从我现有的类创建一个数据表并填充它。 但是我陷入了思考过程中。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Data; namespace Generics { public class Dog { public string Breed { get; set; } public string Name { get; set; } public int legs { get; set; } public bool tail { get; set; } } class Program { […]

linq中的动态表名

我正在尝试使用动态表名执行一些LINQ命令。 例如,而不是: var o = (from x in context.users select x); 我想使用类似的东西: var o = (from x in getTableObjectByName(“users”, context) select x); 或多或少。 这是我到目前为止的代码,它编译和运行: using (MySiteEntities ipe2 = new MySiteEntities()) { var propinfo1 = Type.GetType(“MySiteNamespace.MySiteEntities”).GetProperty(“users”); var propval1 = propinfo1.GetValue(ipe2, null); } 运行,但始终返回零记录。 users表最肯定包含记录,在任何情况下,当我使用上面的第一种方法直接调用它时,我会按预期获得所有记录。 如何修改我的代码以实际记下记录,而不仅仅是空集合? 编辑:我也试过这个: using (MySiteEntities ipe = new MySiteEntities()) { var prop = […]

克隆不同对象属性的最佳方法

我有一个MVC3应用程序需要将视图模型与数据库模型同步。 我发现自己写了太多代码来在不同对象之间来回复制属性。 我避免了这种情况,我可以简单地对数据模型进行子类化,但在其他时候,我发现它太过限制了。 我在Object上开发了一些扩展方法,以支持具有相似名称的属性的浅层克隆,并且这种方法运行得相当好。 但是,我想知道是否有更有效的方法来完成同样的事情。 所以我想这是要求同行评审和选项来改进这个代码。 更新:我发现明确处理相关表格更好。 对IsVirtual进行测试可以防止在克隆过程中无意中影响关系。 请参阅更新的CloneMatching方法。 其他人明确说明要更新或排除的属性。 public static class CustomExtensions { public static T CloneMatching(this T target, S source) where T : class where S : class { if (source == null) { return target; } Type sourceType = typeof(S); Type targetType = typeof(T); BindingFlags flags = BindingFlags.IgnoreCase | BindingFlags.Public | […]

递归获取类的属性和子属性

我正在做一些像递归获取对象的属性和子属性的东西,但我想递归地使用reflection来获取每个属性。 我从Recursively打印属性的代码。 代码的问题是:它只降低了一级,我想知道如何使用reflection自动获取所有属性? 我刚刚编写了以下示例Container代码: public class Container { public Bottle MyBottle { get; set; } public List Addresses { get; set; } public Container() { Address a = new Address(); a.AddressLine1 = “1 Main St”; a.AddressLine2 = “2 Main St”; Addresses = new List(); Addresses.Add(a); MyBottle = new Bottle(); MyBottle.BottleName = “Big bottle”; MyBottle.BottageAge = […]

C#使用reflection获取parms的值

如何获取parms的值(使用reflection循环)。 在之前的问题中,有人向我展示了如何使用reflection来遍历参数。 static void Main(string[] args) { ManyParms(“a”,”b”,”c”,10,20,true,”end”); Console.ReadLine(); } static void ManyParms(string a, string b, string c, int d, short e, bool f, string g) { var parameters = MethodBase.GetCurrentMethod().GetParameters(); foreach (ParameterInfo parameter in parameters) { string parmName = parameter.Name; Console.WriteLine(parmName); //Following idea required an object first //Type t = this.GetType(); //t.GetField(parmName).GetValue(theObject)); } } […]

如何获取接口方法的MethodInfo,实现类方法的MethodInfo?

我有一个接口方法的MethodInfo和一个实现接口的类的 Type 。 我想找到实现接口方法的类方法的MethodInfo 。 简单的方法method.GetBaseDefinition()不适用于接口方法。 按名称查找也不起作用,因为在显式实现接口方法时,它可以有任何名称(是的,不在C#中)。 那么正确的做法是什么,涵盖所有可能性?

如何在C#中为类动态添加字段?

有没有办法在运行时向类添加Field (或FieldInfo ,也许这是相同的)?