reflection

How to get all names and values of any object using reflection and recursion

心已入冬 提交于 2020-01-25 05:39:03
问题 I am trying to get a property names and values from an instance of an object. I need it to work for objects that contain nested objects where I can simple pass in the the parent instance. For example, if I have: public class ParentObject { public string ParentName { get; set; } public NestedObject Nested { get; set; } } public class NestedObject { public string NestedName { get; set; } } // in main var parent = new ParentObject(); parent.ParentName = "parent"; parent.Nested = new NestedObject

Java - Reflection, casting to an unknown Object?

拈花ヽ惹草 提交于 2020-01-25 05:09:04
问题 Basically, I scan through all components in a JFrame, checking if it has the method setTitle(String arg0), if it does, then set it's title to "foo". However, in order to set it's title I need to cast it to a suitable object. public void updateTitle(Container root){ for (Component c : root.getComponents()){ String s = ""; for (Method m : c.getClass().getDeclaredMethods()){ s += m.getName(); } if (s.contains("setTitle")){ c.setTitle("foo"); //Here is where I need the casting } if (c instanceof

c# get parent from chlid instance

∥☆過路亽.° 提交于 2020-01-25 03:35:06
问题 I try to get the parent Type of a instance. How can I do ? Example : public class a { public b { get; set; } } public class b { } var a = new a(); a.b = new b(); var parentType = a.b.??GetParentInstanceType()?? 回答1: You can't. You'd need to add a property to the child manually to keep track of the parent: Here is one way: public class A { public B<A> Child { get; set; } } public class B<T> { public T Parent { get; set; } } A a = new A(); a.Child = new B<A>(); a.Child.Parent = a; Type

IsAssignableFrom with COM

别说谁变了你拦得住时间么 提交于 2020-01-25 01:52:15
问题 I'm working with a COM-API, Autodesk Inventor. This test passes: [Test] public void CastTest() { Inventor.Document document = _application.ActiveDocument; var assemblyDocument = (Inventor.AssemblyDocument)document; Assert.NotNull(assemblyDocument); } This test fails: [Test] public void IsAssignableFromTest() { Assert.IsTrue(typeof(Inventor.Document).IsAssignableFrom(typeof(Inventor.AssemblyDocument))); } I don't know much about COM at all, is there a way to check if one COM type "inherits"

Loading dll that contains Newtonsoft assembly with reflection error

非 Y 不嫁゛ 提交于 2020-01-24 23:29:05
问题 I'm trying to load an assembly using reflection that uses the ISerializationBinder binder from Newtonsoft, but when the assembly is loaded it throws the following exception: System.Reflection.ReflectionTypeLoadException: 'Unable to load one or more of the requested types. Could not load type 'Newtonsoft.Json.SerializationBinder' from assembly 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.' I don't really have a clue of why it could be 回答1: Well,

Set property on a derived class of List<T> instantiated at runtime using reflection

拜拜、爱过 提交于 2020-01-24 19:59:05
问题 I am using reflection to instantiate a custom class `class MyList : MyBaseClass, IList´: foreach (FieldInfo field in this.GetType().GetFields()) { if (field.FieldType.IsGenericType && field.FieldType.GetGenericTypeDefinition() == typeof(MyList<>)) { var elementType = field.FieldType.GetGenericArguments()[0]; var listType = typeof(MyList<>).MakeGenericType(elementType); var valueToSet= Activator.CreateInstance(listType); field.SetValue(this, valueToSet); ... valueToSet.MyName = field.Name; //

Interposing on Java Class Methods (without interfaces)

情到浓时终转凉″ 提交于 2020-01-24 19:56:08
问题 I'd like to interpose between class methods to dynamically extends an object. I already know about the java.lang.reflect.Proxy stuff, but it's way too limited to do real interposing. From Using java.lang.reflect.Proxy to Interpose on Java Class Methods, the first limitation is : (...) the method must be called through an instance of the proxy class. So nested methods calls, for instance, would not be intercepted. And the worst one : (...) the method must have been defined in an Interface that

Building dynamic lambda predicate with short date

瘦欲@ 提交于 2020-01-24 19:50:27
问题 I have the following code that helps me build a lambda expression via reflection. However when I try to compare versus a Date it converts my value to a full DateTime stamp. How can I get it to build my predicate so it will only compare the short date? System.Reflection.PropertyInfo propInfo = typeof(T).GetProperty(property); Type propertyType = propInfo.PropertyType; if (Utilities.IsNullableType(propertyType)) { propertyType = Nullable.GetUnderlyingType(propertyType); } ParameterExpression

How to diff Property Values of two objects using GetType GetValue?

泄露秘密 提交于 2020-01-24 19:03:42
问题 I have the following classes: public class Person { public String FirstName { set; get; } public String LastName { set; get; } public Role Role { set; get; } } public class Role { public String Description { set; get; } public Double Salary { set; get; } public Boolean HasBonus { set; get; } } I want to be able to automatically extract the property value diferences between Person1 and Person2, example as below: public static List<String> DiffObjectsProperties(T a, T b) { List<String>

Accessing Field Variable from Generic Object

拥有回忆 提交于 2020-01-24 16:32:39
问题 I have two classes, ClassOne and ClassTwo , that update a public field data i.e., public class ClassOne { public byte[] data = new byte[10]; // Thread that updates data } and public class ClassTwo { public byte[] data = new byte[10]; // Thread that updates data } Only one class and its associated thread is running at any given time, i.e., the app detects the source of the data at run time and uses either ClassOne or ClassTwo . I want to parse the data in a separate class called ParseMyData ,