reflection

Using NSCoder and NSKeyedArchiver with runtime reflection to deep copy a custom class

僤鯓⒐⒋嵵緔 提交于 2019-12-25 04:16:05
问题 I'd like to use - (id)initWithCoder:(NSCoder *)coder and - (void)encodeWithCoder:(NSCoder *)coder to encode a custom class for copying (using NSKeyedArchiver etc) My custom class contains a large number of iVars. Instead of listing all of these in the encoding code I'd like to use reflection to simply enumerate over all the properties in the custom class and encode/decode each property from within a loop. I'll end up with code that looks a bit like this: - (id)initWithCoder:(NSCoder *)coder {

MethodAccessException with Reflection on Windows Phone 7

醉酒当歌 提交于 2019-12-25 04:14:17
问题 While working with reflection, I recently got to the point where I wanted to access an object (in fact, a static instance of an object). The object itself is defined by an internal class, therefore there is no other way to access it. Instead of directly getting a parametrized constructor, I can access a static instance via the Instance property. With the help of reflection, I am also able to get this property and set it to a PropertyInfo instance - it is detected correctly. However, I am not

Handle every get or set call to objects properties

橙三吉。 提交于 2019-12-25 04:07:28
问题 I am searching for a good approach to automatically check the access rights when accessing a propertys getter and setter. This is just for curiosity and experimental purposes, so performance does not really matter. One Approach I found is the following (example is just for getters): public class DynamicPropertyClass : DynamicObject { /// <summary> /// I want this to work for a default auto-property /// </summary> public string TestString { get; set; } public override bool TryGetMember

How to extend a method at runtime?

六月ゝ 毕业季﹏ 提交于 2019-12-25 04:00:19
问题 Here is the class: class Foo { private void Boo() { // Body... } // Other members... } What I need is: Create a Foo2 class at runtime which has a copy of all Foo class members. In Foo2 class replace Boo method by Boo2 method that has its content alternated to some extent. Create an instance of Foo2 and invoke Boo2 . Thank you for help. 回答1: You can do it at runtime using a .NET AOP Framework event if it is not the the main purpose of this kind of framework. I actively work on a new one which

Using System.Reflection

丶灬走出姿态 提交于 2019-12-25 03:47:07
问题 Im loading an Exe into my application using Assembly.LoadFile(). From that is it possible to get the Method of a particular class from that EXE. Thanks in advance . 回答1: Try: var assembly = Assembly.LoadFile("C:\path-to-some-app.exe"); var desiredType = assembly.GetType("SomeNamespace.SomeClass"); var methodInfo = desiredType.GetMethod("MethodName"); 回答2: A number of things you can do with Reflection (Including constructors, method invocation etc.) Tutorial at : http://www.csharp-examples.net

Verifying code against template patterns using reflection

感情迁移 提交于 2019-12-25 03:41:34
问题 I am working on a large project where a base class has thousands of classes derived from it (multiple developers are working on them). Each class is expected to override a set of methods. I first generated these thousands of class files with a code template that conforms to an acceptable pattern. I am now writing unit tests to ensure that developers have not deviated from this pattern. Here is a sample generated class: // Base class. public abstract partial class BaseClass { protected

Follow Up: Create an an array of objects from classname

喜欢而已 提交于 2019-12-25 03:04:40
问题 I am following up on this question, 1268817 In the question, we find a way to create an isntance of an object given a the name (as a string) of the class. But what about to create an array of those objects... how would one initialize that. I was thinking something in the line of but doesnt seem to work Object[] xyz = Class.forName(className).newInstance()[]; 回答1: Object objects = java.lang.reflect.Array.newInstance(Class.forName(classname), 10); For an array of 10 elements. Annoyingly it

Why does Scala reflection work inside an object but not at the top level of a script?

百般思念 提交于 2019-12-25 02:58:54
问题 This script uses reflection to find the type signature of a constructor. It contains the same code inside an object and at the top level: // Scala 2.11.1 case class Dirigible(cubicFeet: Int) object Object { val u = scala.reflect.runtime.universe val ctor = u.weakTypeOf[Dirigible].decl(u.termNames.CONSTRUCTOR).typeSignature def run() { println(ctor) } } Object.run() val u = scala.reflect.runtime.universe val ctor = u.weakTypeOf[Dirigible].decl(u.termNames.CONSTRUCTOR).typeSignature println

Reflection instantiation

别来无恙 提交于 2019-12-25 02:47:39
问题 I am currently trying to create a generic instance factory for which takes an interface as the generic parameter (enforced in the constructor) and then lets you get instantiated objects which implement that interface from all types in all loaded assemblies. The current implementation is as follows: public class InstantiationFactory<T> { protected Type Type { get; set; } public InstantiationFactory() { this.Type = typeof(T); if (!this.Type.IsInterface) { // is there a more descriptive

Using Java Reflection to initialize member variables

只愿长相守 提交于 2019-12-25 02:46:13
问题 I'm hoping someone can point in the direction of some useful information pertaining to the best practices surrounding the use of Reflection in Java. The current project I'm supporting uses Oracle ADF Faces and we've found that based on the objectives at hand certain pages end up with a vast number of components which need to be initialized in the backing beans. One of the developers on the team devised a solution using reflection in the bean constructor to initialize all of the member