Tag: reflection

如何通过动态reflection在运行时创建RelayCommand实例?

我正在WPF中构建MVVM应用程序,我将Menu绑定到MenuItem模型。 我的MenuItem类有以下属性: public class MenuItem { private List _Items; public MenuItem(string header, ICommand command) { Header = header; Command = command; } public MenuItem() { } public string Header { get; set; } public List Items { get { return _Items ?? (_Items = new List()); } set { _Items = value; } } public […]

反思与应用设计

让我来描述一下我现在遇到的问题和解决方案,也许你可以帮助我了解为什么这是一个坏主意(假设它是)以及我能做些什么来使它成为一个更好的系统。 现在我有600个“裂土器”解析文件并将它们翻录成csv或其他格式。 裂土器都实现了一个通用的接口和基类。 当作业排队时,取决于该作业的配置,使用reflection调用特定的开膛手。 foreach (var stream in streams) { try { // load it Assembly asm = Assembly.LoadFile(Path.Combine(stream.RipperPath, stream.RipperFileName), new Evidence()); if (asm == null) { MessageBox.Show(String.Format(“Invalid Interface ripper loaded.\n{0}”, Path.Combine(stream.RipperPath, stream.RipperFileName))); return; } foreach (Type objType in asm.GetTypes()) { if (!objType.IsPublic) continue; // make sure the type isn’t Abstract if (((objType.Attributes & TypeAttributes.Abstract) […]

C#reflection:从类型化数据集中获取DataRow的字段

我目前正在构建一个方法,该方法从类型化的DataSet中获取DataRow类型的对象,然后返回DataRow中字段的JSON格式的字符串(用于Web服务)。 通过使用System.Reflection ,我正在做这样的事情: public string getJson(DataRow r) { Type controlType = r.GetType(); PropertyInfo[] props = controlType.GetProperties(); foreach (PropertyInfo controlProperty in props) { } return “”; } 然后在foreach语句中,我将迭代每个字段并获取字段名称和值,并将其格式化为JSON。 问题是当迭代props (类型为PropertyInfo[] )时,我得到的属性我不想被迭代: 替代文字http://img88.imageshack.us/img88/2001/datarowreflectionht0.gif 从上图中可以看出,我只需要props数组中0 – 11的字段,因为这些是这个特定类型行的“真实字段”。 所以我的问题是, 如何只获取Typed DataRow的字段,而不是其他’元数据’? [更新解决方案] 正如Mehrdad Afshari建议的那样,我使用的是Table.Columns数组,而不是使用Reflection 。 这是完成的function: public string GetJson(DataRow r) { int index = 0; StringBuilder json = new […]

按惯例/reflection动态连接视图,模型和演示者

我正在尝试使用MVP模式开发应用程序。 问题是手动连接所有代码。 我希望减少所需的代码。 我试图复制另一个解决方案,但我无法开始工作。 我正在使用Winforms,我使用的解决方案是使用WPF。 它会在某些约定上汇总事物: 视图事件按名称连接。 例如:视图上的“已加载”事件将连接到演示者上的“OnLoaded()”方法按钮命令按名称连接。 例如:MoveNext“按钮连接到”OnMoveNext()“方法。网格双击按名称连接。例如:双击”Actions“将连接到”OnActionsChoosen(ToDoAction)“ WPF中的工作代码是: private static void WireListBoxesDoubleClick(IPresenter presenter) { var presenterType = presenter.GetType(); var methodsAndListBoxes = from method in GetActionMethods(presenterType) where method.Name.EndsWith(“Choosen”) where method.GetParameters().Length == 1 let elementName = method.Name.Substring(2, method.Name.Length – 2 /*On*/- 7 /*Choosen*/) let matchingListBox = LogicalTreeHelper.FindLogicalNode(presenter.View, elementName) as ListBox where matchingListBox != null select […]

反思的替代品

我对Generics和Reflection的经验非常少。 我从以下示例中假设的是,它需要花费太多时间来执行。 有没有办法让我在不使用reflection的情况下完成以下操作。 SCENARIO我正在研究一种通用的方法。 它需要传递给它的类的实例,并从所有属性中生成SqlParameters。 以下是名为“Store”的generics方法的代码,以及另一种将c#type转换为DbType的SqlDbType的方法。 List parameters = new List(); public T Store(T t) { Type type = t.GetType(); PropertyInfo[] props = (t.GetType()).GetProperties(); foreach (PropertyInfo p in props) { SqlParameter param = new SqlParameter(); Type propType = p.PropertyType; if (propType.BaseType.Name.Equals(“ValueType”) || propType.BaseType.Name.Equals(“Array”)) { param.SqlDbType = GetDBType(propType); //eg public bool enabled{get;set;} OR public byte[] img{get;set;} […]

动态创建IList类型的新实例

我的申请正在处理IList。 不同用户定义类型的IList。 我想我可以使用reflection来查看IList包含的对象类型,然后创建该类型的新实例,然后将其添加到IList本身? 所以在任何时候我都可能正在处理 IList l; 我想创建一个新的Customer实例 Customer c = new Customer(0, “None”) 然后将其添加到列表中 l.Add(c); 显然,在运行时动态执行此操作是问题的关键。 希望有人能给我一些指示。 谢谢brendan

C#如何使用“Type”类型的对象初始化Generic类

我最近有这个问题。 doSomething(typeof(int)); doSomething(typeof(MyClassA)); doSomething(typeof(MyClassB)); public void doSomething(Type _type) { var myGenObj = new MyGenericClass(); // Error. Really I’d want MyGenericClass, MyGenericClass, etc depending on what’s passed in. myGenObj.doSomeGenStuff(); // more stuff… } 我认为这可以通过某种方式进行反思。可能有一种更简单的方法。 关于Type的工作方式和类下的Classes,我有点困惑。 无论如何,谢谢你的帮助。 谢谢。

使用包含.Net中变量名称的字符串访问方法局部变量值

我试图获取已在基于包含变量名称的字符串的方法中本地声明的变量的值。 我一直在尝试使用多个SO线程中发布的reflection。 变量的名称存储在表中,并以相同的方法访问。 我遇到的问题是看到方法局部变量或方法本身。 我可以看到下面的代码片段中指出的全局变量的值,但我在我的方法中找不到局部变量。 换句话说,变量TestrecordType被声明为类变量,我可以在任何方法中使用下面的代码来访问它。 我需要访问代码所在的本地方法中的变量。 任何人都可以帮助我深入到方法级别吗? 可能吗? //10.07.2013 DRowe Collecting data for the json class Corporate_Record clsCorporateRecord = new Corporate_Record(); // get the mapping for the class ABC.Services.SYS.BmsBoardingEntryLayoutSpecificationCollection colLayoutSpecs = new ABC.Services.SYS.BmsBoardingEntryLayoutSpecificationCollection(); colLayoutSpecs = ABC.Services.SYS.BmsBoardingEntryLayoutSpecification.GetBySubClass(“Corporate_Record”); foreach (ABC.Services.SYS.BmsBoardingEntryLayoutSpecification SpecRecord in colLayoutSpecs) { // Alter main class level (Global) variable TestrecordType = TestrecordType + ” […]

将正确的类型参数传递给MethodInfo GetMethod

我知道我可以使用像这个问题这样的技术来获得我的方法。 例如 MethodInfo firstMethod = typeof(Enumerable) .GetMethods(BindingFlags.Public | BindingFlags.Static) .First(m => m.Name == “FirstOrDefault” && m.GetParameters().Length == 1) 我想简化那个过程。 我正在寻找Enumerable.FirstOrDefault方法(IEnumerable) 我试过了两个 // I’m just using string just as an example. var enumerableType = typeof(IEnumerable).MakeGenericType(typeof(string)); MethodInfo firstMethod = typeof(Enumerable) .GetMethod(“FirstOrDefault”, new Type[] { enumerableType }); 和 MethodInfo firstMethod = typeof(Enumerable) .GetMethod(“FirstOrDefault”, Type.EmptyTypes); 但两者都返回null 。 什么是正确的方法?

动态选项对话框(使用reflection)

有没有人知道一个好的组件(C#WinForms)允许创建一个选项(设置)表单,给定一个具有一堆属性的自定义类? 我不是在寻找一些有光泽的东西,而是一些仅仅比房产网格更好的东西。 我可以很容易地处理视觉部分,但我只是不想浪费时间做reflection添加和绑定控件(如果它已经存在)。 我很确定我以前在某处看到过类似Visual Studio的选项,它是动态创建的(有一些属性附加到类的属性,以允许分组和其他信息)。 [编辑]例如,我可能有一个选项类: public class Options : SerializableOptions { [Category(“General”)] [Name(“User name”)] [Description(“Some text”)] public string Username { get; set; } [Category(“General”)] [Name(“Log in automatically”)] public bool LogInAutomatically { get; set; } [Category(“Advanced”)] // ConnectionType is enum public ConnectionType ConnectionType { get; set; } // … } 将它传递给这个表单后,它将创建两个面板(“常规”和“高级”),第一个面板上有一个CheckBox和一个TextBox,第二个面板上有一个ComboBox(带有所有可用的枚举)。 如果没有这样的控制,你们用什么? 为每个选项手动添加,填充,格式化和绑定控件?