reflection

How to learn whether an interface is derived from a specific interface?

Hi, I have an interface like that: public interface IViewA : IViewB, IViewC { byte prop { get; set; } } and I have a generic method like that: public void OpenPopup<T>(WindowState state) { if ((typeof(T) as IViewC)!=null) { //Process A } else { //Process B } } Although I send T as a...

How to "slice" a POJO

I'm borrowing the "slice" meaning from C++. Let's say I hava a simple POJO that's persisted via Hibernate: class Person { private long id; private String name; ... // getters and setters here ... } Now, when I retrieve an object from the database I know it was "instrumented" by Hibernate (its real class is a Person-derived generated ...

convert HashMap to JavaBean instance given javabean class

Hi, is there a utility out there for creating a bean (which has simple String attributes whose name match with HashMap keys ) given bean class as input along with properly set hashmap? thanks, Amit ...

Why are pointers reference types?

This week I've been working on some reflection-based code, and during unit testing found an unexpected condition: pointers are reference types. The C# code typeof(int).MakePointerType().IsClass returns true. I checked in my just-arrived Annotated CLI Standard, and sure enough, pointers are clearly defined as reference types. This was s...

How do I determine if a method is a generic instance of a generic method

Hi all, I have a MethodInfo passed in to a function and I want to do the following MethodInfo containsMethod = typeof(ICollection<>).GetMethod("Contains"); if (methodInfo.Equals(containsMethod) { // do something } But this doesn't work because the methodInfo has a specific generic type. For the example does work if I knew that th...

How can i access a server side control from asp.net code behind file using reflection?

For example, if i have on the aspx page: <asp:PlaceHolder ID="tab_0" runat="server" Visible="false"></asp:PlaceHolder> <asp:PlaceHolder ID="tab_1" runat="server" Visible="false"></asp:PlaceHolder> and i want to access these properties in the code behind page using values from a configuration file for example string enabledTabs = "0,...

Creating an object that's defined in a dll that I don't have a reference to

I have the full path to a dll, but not a reference, where I need to instantiate an object that implements an interface that I've defined. I do have control over the other dll, so I am able to do things like stick a static function in the dll that returns the object I need. I'm just not sure how to call that function, or even if I'm appro...

How to retrieve the value of a Custom Attribute from a DLL Loaded at runtime?

I have a app that requires a dll to be loaded at runtime and I want to create some Custom Attributes in the dynamically loaded DLL so when it is loaded I can check to make sure that certain attributes have certain values before trying to use it. I create an attribute like this using System; [AttributeUsage(AttributeTargets.Class)] publ...

Recursively walking a Python inheritance tree at run-time

Hi, I'm writing some serialization/deserialization code in Python that will read/write an inheritance hierarchy from some JSON. The exact composition will not be known until the request is sent in. So, I deem the elegant solution to recursively introspect the Python class hierarchy to be emitted and then, on the way back up through the ...

What are some appropriate and inappropriate uses of reflection in Java?

Java's reflection API is obviously a very powerful tool, but not particularly object-oriented. What are some situations where it is appropriate (and conversely, inappropriate) to use reflection? ...

What resources explain Reflection and Meta Programming in C#?

I've been programming in C# for a while now and would say I can use it and the .Net library fairly well. One huge gap in my knowledge though is how to use Reflection and Meta Programming with .Net/C#. Can someone point me to some good resources on the subject and how to get started with it? ...

Creating an expression tree that calls a method

Is it possible to create an expression tree that directly calls a method? For example, consider the following method: public static int MyFunc(int a, int b) { return a + b; } I would like to create an expression tree that calls MyFunc with parameters a=1 and b=2. One way to accomplish this is with reflection: var c1 = Expression....

In Squeak, where do I find the code for the message handling algorithm?

When sending a message to an object in Squeak, the runtime invocation algorithm is something like curr <- the receiver's class Repeat while curr isn't nil Search for the selector in that class's methods; if it's there, invoke it and return curr <- curr's superclass Call doesNotUnderstand: on self Now, a very similar algorithm is us...

Problem creating type with Reflection

Hello, I got a following base class: public class ValidationItem { public ObservableCollection<object> GetFilteredValues( ObservableCollection<object> values) { return new ObservableCollection<object>(); // nothing here yet } } I create a type which inherits this base type and I create a getter which is going to ...

How to determine whether an interface type implements a custom attibute

Hi, How to determine whether an interface type implements a custom attibute? ...

Where is the information about the entry point of an assembly written in the assembly?

I used to think that an assembly could have only one main() method until I saw Jon Skeet's MiscUtil in a video lecture he delivered at the Microsoft office in Copenhagen. So, I wrote this little app that had two main() methods like so: namespace ManyMains { class Program { static void Main(string[] args) { ...

Why do IsAssignableFrom and GetInterface give different results.

I have created a type like this: TypeBuilder tb = moduleBuilder.DefineType(myname, TypeAttributes.Class | TypeAttributes.Public, typeof(BaseClass), new Type[] { typeof(ImyInterface) }); Then lots of ilgenerating code follows for constructors, methods etc. When I start using the class I noticed something strange. I want to check...

get class from java.util.List<SomeType>

how to get class from this expression java.util.List ...

Checking if an annotation is of a specific type

I am using reflection to see if an annotation that is attached to a property of a class, is of a specific type. Current I am doing: if("javax.validation.Valid".equals(annotation.annotationType().getName())) { ... } Which strikes me as a little kludgey because it relies on a string that is a fully-qualified class-name. If the namesp...

Problem loading assembly via Reflection and accessing configuration

I'm trying to load a .NET assembly with Reflection (using the Assembly.LoadFrom method), and instantiate some types within this assembly. This all seems to work fine, but one type I'm trying to instantiate accesses the assembly's configuration within its type initializer: it does ConfigurationManager.GetSection(sectionName). The assemb...