reflection

How to dynamically load a Python class

Given a string of a Python class, e.g. 'my_package.my_module.MyClass', what is the best possible way to load it? In other words I am looking for a Class.forName() function in Python. It needs to work on Google App Engine. Preferably this would be a function that accepts the FQN of the class as a string, and returns a reference to the c...

Hibernate implementation. Are we paying the reflection penalty?

Long time ago, I was creating a mini ORM using reflection. While reading about reflection I got a similar answer like this: Java Reflection Performance Which makes completely sense and I quit my mini orm and sharpen my CTRL+C, CTRL+V keys ( the lib was intended to avoid having to rewrite again and again the same snippets for differen...

How would you refactor a "switch on Type" to be polymorphic if you don't control the types involved?

I'm writing a microformats parser in C# and am looking for some refactoring advice. This is probably the first "real" project I've attempted in C# for some time (I program almost exclusively in VB6 at my day job), so I have the feeling this question may become the first in a series ;-) Let me provide some background about what I have so...

C# Reflection & Generics

Hi, Got a complex reflection question. Given the code below how would you implement the pseudo so that given an instance of Parent it will enumerate over the types Properties find child objects with a Property of the same type as Parent and set the reference to the provided p. Hope that makes sense. Also I need this to work with Generic...

Can you use a string to instantiate a class in python?

I'm using a builder pattern to seperate a bunch of different configuration possibilities. Basically, I have a bunch of classes that are named an ID (something like ID12345). These all inherit from the base builder class. In my script, I need to instantiate an instance for each class (about 50) every time this app runs. So, I'm trying to ...

How can I cause a setter method to encrypt data before setting it in ActiveRecord?

I'd like to implement a Rails User model that has a DB column called password. I want to make it so that when I call... user_instance.password = 'cleartext' the method hashes the cleartext before setting it on the instance like so: Digest::SHA1.hexdigest(cleartext) I've tried using a callback, but the problem is that is hashes the ...

Cast with GetType()

Is it possible to cast an object to the type returned from GetType()? I'd like a generic method that can accept an object (for anonymous types) but then return an object cast as the anonymous type. I've been thinking of using the LCG DynamicMethod to build a method on a container class, but I can't exactly figure out what that would look...

Retrieving type parameters from an instance of a generic base interface

Hello, Given 2 interfaces: public interface BaseInterface<T> { } public interface ExtendedInterface<T0, T1> extends BaseInterface<T0> {} and a concrete class: public class MyClass implements ExtendedInterface<String, Object> { } How do I find out the type parameter passed to the BaseInterface interface? (I can retrieve the Extend...

How can I access my assembly version number in a partial trust environment (no FileIOPermission)

I'm trying to access the Version number of my assembly at runtime. The code I'm using for that requires a FileIOPermission, which I don't want to grant (I'm in the Internet Zone) this.GetType().Assembly.GetName().Version; Is there another way to access the version number which doesn't require elevation? ...

Reflection - SetValue of array within class?

OK, I've been working on something for a while now, using reflection to accomplish a lot of what I need to do, but I've hit a bit of a stumbling block... I'm trying to use reflection to populate the properties of an array of a child property... not sure that's clear, so it's probably best explained in code: Parent Class: Public Class ...

java and reflection

Is it possible to call a method by reflection from a class? class MyObject { ... //some methods public void fce() { //call another method of this object via reflection? } } Thank you. ...

Implementing a Factory that uses Specifications to determine which type of object to create

This is mainly a thought experiment. So this all is sample code. My goal was to use the specification pattern to eliminate giant blocks of conditional code inside a factory. So with this sample I have a StatusData object that I want to get an implementation of IStatusUpdate that is appropriate for for it. I have the following set of tes...

How to deep copy between objects of different types in C#.NET

I have a requirement to map all of the field values and child collections between ObjectV1 and ObjectV2 by field name. ObjectV2 is in a different namspace to ObjectV1. Inheritence between the template ClassV1 and ClassV2 has been discounted as these 2 classes need to evolve independently. I have considered using both reflection (which ...

How do I intercept a method invocation with standard java features (no AspectJ etc)?

I want to intercept all method invocations to some class MyClass to be able to react on some setter-invocations. I tried to use dynamic proxies, but as far as I know, this only works for classes implementing some interface. But MyClass does not have such an interface. Is there any other way, besides implementing a wrapper class, that d...

How can I port this Java code to C#?

I am looking at a pattern implemented in Java and have some questions about how it aligns to (can be ported to) C#. Java: class Foo { private Class someClass; ... } class Bar { private Field some Field; } First, Class stores an instance of a domain object. It looks like Java exposes reflection methods on the type which ...

Can I get specific metadata from a Func<T, object>?

Consider the following code: string propertyName; var dateList = new List<DateTime>() { DateTime.Now }; propertyName = dateList.GetPropertyName(dateTimeObject => dateTimeObject.Hour); // I want the propertyName variable to now contain the string "Hour" Here is the extension method: public static string GetPropertyName<T>(this IList<...

Protocol buffer deserialization and a dynamically loaded DLL.

I am using protobuf-net for my protocol buffering. I have a dll I am loading dynamically. I can create an instance of a data class contained within the dll, and I can use and modify the created data object. However, When I attempt to serialize/deserialize the data object I get the following crash: {"Unable to identify known-type for Pro...

Test whether a property is declared in the derived class

I have two classes public class A { public int BaseA {get;set;} } public Class B: A { public int BaseB {get;set;} } I can get the Properties for the Class B by using typeof(B).GetProperties(). However, this would include both the BaseA and BaseB properties. But I want to obtain the BaseB property only. Note: I found the solution...

C# referencing desired overloaded generic method

given public Class Example { public static void Foo< T>(int ID){} public static void Foo< T,U>(int ID){} } Questions: 1) Is it correct to call this an "overloaded generic method"? 2) How can either method be specified in creation of a MethodInfo object? Type exampleType = Type.GetType("fullyqualifiednameOfExample, namespaceOfE...

Compiling a class at runtime failes when CompilerParameters.GenerateInMemory == true

Hello, I am compiling a dynamic assembly at runtime. It needs to reference another dll. Everything works alright, as long as I set an OutputAssembly in my CompilerParameters. But as soon as I set GenerateInMemory = true; it failes: var compilerParameters = new CompilerParameters(); if( compileInMemory ) compilerParameters.GenerateI...