Tag: reflection

创建从IBlahblahinheritance的所有类的集合

使用reflection(我猜?),是否有可能创建一个方法来返回从名为IBlahblah的接口inheritance的所有对象的集合? public interface IBlahblah;

如何在编译时创建List 并通过System.Reflection.PropertyInfo复制项目

我遇到了一些相当复杂的东西。 如果有人能提供帮助,我将不得不承担责任。 1)我必须在编译时创建一个未知类型的List 。 我已经实现了。 Type customList = typeof(List).MakeGenericType(tempType); object objectList = (List)Activator.CreateInstance(customList); “temptype”是已经获取的自定义类型。 2)现在我有PropertyInfo对象,我必须将所有项目复制到我刚刚创建的实例“objectList” 3)然后我需要迭代并访问“objectList”的项目,就像它是“System.Generic.List”一样。 简而言之,使用reflection我需要提取一个列表属性并将其作为实例供进一步使用。 您的建议将不胜感激。 提前致谢。 Umair

获取PropertyInfo的递归例程

我正在尝试创建一个递归例程,它将为指定对象下的所有成员检索PropertyInfos(在.NET 3.5中)。 直接成员的所有东西都在工作,但它也需要解析嵌套类(以及它们的嵌套类等)。 我不明白如何处理解析嵌套类的部分。 你会如何写这部分代码? public class ObjectWalkerEntity { public object Value { get; set; } public PropertyInfo PropertyInfo { get; set; } } public static class ObjectWalker { // This will be the returned object static List objectList = new List(); public static List Walk(object o) { objectList.Clear(); processObject(o); return objectList; } private static […]

TypeDescriptor不会从inheritance的接口返回成员

我的问题是TypeDescriptor不会从inheritance的接口返回成员,这是它应该如何工作? 还是一个bug? [TestFixture] public class DescriptorTests { [Test] public void Test() { // count = 1 Assert.AreEqual(2, TypeDescriptor.GetProperties(typeof(IFoo)).Count); // it is going to fail, the Id is not going to be returned } public interface IEntity { int Id { get; set; } } public interface IFoo : IEntity { string Name { get; set; […]

如何枚举实现通用接口的所有项目?

我有两个接口,一个generics和一个非generics,具有inheritance层次结构: public interface IGenericRelation : IRelation public interface IRelation 通用的是由几个动态加载的服务器控件实现的,我希望枚举实现此接口的控件集合。 我可以做以下事情 foreach (IRelation relationControl in this.uiPlhControls.Controls.OfType<IRelation) { … } 但我真正希望能做的是…… foreach (IGenericRelation relationControl in this.uiPlhControls.Controls.OfType<IGenericRelation) { … } 然后能够将relationControl与它提供的类型一起使用,然后我就可以访问IGenericRelation上可用的强类型属性。 不幸的是,这是不可能的,因为我似乎无法省略类型参数。 有没有人知道枚举实现通用接口的控件的方法,以防止我必须编写几个循环而不是一个循环? 或许使用reflection?

使用reflection通过从setter调用的方法获取属性的属性

注意:这是对上一个问题的答案的后续跟进 。 我正在使用名为TestMaxStringLength的属性来装饰属性的setter,该属性在setter调用的方法中用于validation。 该物业目前看起来像这样: public string CompanyName { get { return this._CompanyName; } [TestMaxStringLength(50)] set { this.ValidateProperty(value); this._CompanyName = value; } } 但我宁愿它看起来像这样: [TestMaxStringLength(50)] public string CompanyName { get { return this._CompanyName; } set { this.ValidateProperty(value); this._CompanyName = value; } } 负责查找setter属性的ValidateProperty的代码是: private void ValidateProperty(string value) { var attributes = new StackTrace() .GetFrame(1) .GetMethod() .GetCustomAttributes(typeof(TestMaxStringLength), […]

在C#中,如何使用reflection访问花括号构造函数?

在C#中,我们现在可以使用花括号构造函数构造新对象,即 class Person { readonly string FirstName {get; set;} readonly string LastName {get; set;} } new Person { FirstName = “Bob”, LastName = “smith” } 我需要使用reflection来构造这个对象,但是如果这些成员变量被标记为只读,我只能在构造函数中设置它们,并且只有花括号构造函数可用。 有什么方法可以使用reflection访问花括号样式的构造函数? 谢谢。

如何通过C#中的类型创建动态委托对象?

假设我有一个type ,我知道它是从Delegate类型派生的。 我想创建一个这种类型的对象包装一个匿名委托,它接受任意参数并返回一个正确返回类型的对象: var retType = type.GetMethod(“Invoke”).ReturnType; var obj = Delegate.CreateDelegate(type, delegate(object[] args) { … if (retType != typeof(void)) … somehow create object of type retType and return it … }); 显然这不会编译,因为CreateDelegate期望一个MethodInfo作为第二个参数。 我该怎么做才能正确? 更新:关于我想要达到的目标的更多信息。 运行了两个应用程序 – 浏览器中的客户端和C#中的服务器。 浏览器能够通过将参数序列化为JSON并通过网络发送呼叫(如RPC)来调用服务器端的远程function。 这已经有效,但我想添加对回调的支持。 例如: JavaScript(客户端): function onNewObject(uuid) { console.log(uuid); } server.notifyAboutNewObjects(onNewObject); C#(服务器): void notifyAboutNewObjects(Action callback) { … callback(“new-object-uuid”); … […]

为什么相同的类型不相等?

我正在开发一个小插件框架,并注意到我无法成功使用Type.IsAssignableFrom()来检查插件类是否实现了类型IPlugin 。 我检查了其他SO问题(比如这个) ,看看为什么函数返回false,但令我惊讶的是,没有一个建议有效。 我在插件程序PluginAPI.IPlugin有一个类,它在引用的程序PluginAPI.IPlugin实现PluginAPI.IPlugin类。 检查我的PluginAPI.IPlugin类型的AssemblyQualifiedName以及插件类的接口列表的类型时,我发现没有任何区别。 两者都超出了价值: PluginAPI.IPlugin,PluginAPI,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null 经过进一步调查,我检查了Type.cs源代码 ,发现函数IsAssignableFrom()在内部调用中失败,它检查类型是否相等(via == )。 我发现修改我的插件框架以在典型的执行上下文( Assembly.LoadFrom )中加载插件程序集而不是仅reflection上下文( Assembly.ReflectionOnlyLoadFrom )允许类型相等性检查评估为true ,这是我所期望的一直。 为什么仅reflection上下文导致类型不再相等? 码: 以下部分包含用于复制问题的相关代码以及已设置的项目说明。 程序集1:PluginAPI 该程序集仅包含IPlugin.cs: namespace PluginAPI { public interface IPlugin { } } 程序集2:Plugin1 该程序集包含几个插件(实现PluginAPI.IPlugin类)。 namespace Plugin1 { public class MyPlugin : IPlugin { public MyPlugin() { } } } […]

用reflection得到领域

我想得到所有具有空值的字段,但我甚至得到任何字段: [Serializable()] public class BaseClass { [OnDeserialized()] internal void OnDeserializedMethod(StreamingContext context) { FixNullString(this); } public void FixNullString(object type) { try { var properties = type.GetType().GetFields(); foreach (var property in from property in properties let oldValue = property.GetValue(type) where oldValue == null select property) { property.SetValue(type, GetDefaultValue(property)); } } catch (Exception) { } } public object […]