reflection

C# Create class instance from string

I have a C# method which creates a new instance of a class from a string, however, I get an error when running the code. obj = (ClassX)Activator.CreateInstance(Type.GetType("classPrefix_" + className)); ArgumentNullException was unhandled Value cannot be null Parameter name: type Any help on this error would be apprecia...

How to invoke methods with ref/out params using reflection

Imagine I have the following class: class Cow { public static bool TryParse(string s, out Cow cow) { ... } } Is it possible to call TryParse via reflection? I know the basics: var type = typeof(Cow); var tryParse = type.GetMethod("TryParse"); var toParse = "..."; var result = (bool)tryParse.Invoke(null, /* what are ...

Generics and reflection in Java

This is probably a very basic question, but I'm really new to generics in Java and I'm having a hard time altering my thought process from the way things are done in C#, so bear with me. I'm trying to build a generic repository in Java. I've created an IRepository interface that looks like this: public interface IRepository<T extends I...

Can C# Attributes access the Target Class?

I want to access the properties of a class from the attribute class by using reflection. Is it possible? For example: class MyAttribute : Attribute { private void AccessTargetClass() { // Do some operations } } [MyAttribute] class TargetClass { } ...

How do I get constants defined by Ruby's Module class via reflection?

I was trying to get Matz-n-Flanagan's RPL book's metaprogramming chapter into my head. However I couldn't understand the output from the following code snippet that I dreamed up. p Module.constants.length # => 88 $snapshot1 = Module.constants class A NAME=:abc $snapshot2 = Module.constants p $snapshot2.length ...

Effective Use of Java Reflection - is this a hack, or is this standard practice?

Hey, long time listener first time caller, and I'm asking a question related to Java reflection, and how easy it lends itself to apparently ugly coding. The following method attempts to take two similar objects (one object having all of the fields of the other object, and then some) and compare the two of them for equality. It will (al...

Real world uses of Reflection.Emit

In all the books I've read on reflection they often say that there aren't many cases where you want to generate IL on the fly, but they don't give any examples of where it does make sense. After seeing Reflection.Emit as a job requirement for a gaming company I was curious where else it's being used. I'm now wondering if there are an...

IsAssignableFrom, IsInstanceOf and BaseType testing

It's just a service locater type of pattern I am trying to implement, where I'd like to catch an attempt to register an implementation to an interface it doesn't belong to, as in: public void Add(Type interfaceType, object implementingObject) { // ... check for nulls // NO GOOD if(!implementingObject.GetType().I...

Get Method Name Using Lambda Expression

I'm trying to get the name of a method on a type using a lambda expression. I'm using Windows Identity Foundation and need to define access policies with the type name with namespace as a resource and the method name as the action. Here is an example. This is the type I would be getting the type name and method name from: namespace My....

Is there a way for a SecurityManager in java to selectively grant ReflectPermission("suppressAccessChecks")?

Is there any way for a SecurityManager in Java to selectively grant ReflectPermission("suppressAccessChecks") depending on the details of what setAccessible() is being called on? I don't see any way for this to be done. For some sandboxed code, it would be very useful (such as for running various dynamic JVM languages) to allow the setA...

Setting field values to another instance of the same Class

class Foo(){ private String x,y; //getters setters } main(){ Foo bar1 = new Foo(); Foo bar2 = new Foo(); bar1.setX("hey"); bar2.setX("hi"); bar2.setY(" there"); setNewValuesFromLeftToRight(bar1,bar2);//left:bar1 System.out.print(bar2.getX+bar2.getY)// hey there } setNewValuesFromLeftToRight : this method wou...

Determine if object is any Predicate<T>

I've got an IList<Delegate> that contains some Func<bool>s and some Predicate<T>s, where T varies. I later need to sort out which of these items are Predicate<T>s, but don't want to close the door to adding other Delegate types to the list later, so I do not want to do this by identifying objects by !(current_delegate is Func<bool>). Th...

Creating classes dynamically with Java

I have tried to find information about this but have come up empty handed: I gather it is possible to create a class dynamically in Java using reflection or proxies but I can't find out how. I'm implementing a simple database framework where I create the SQL queries using reflection. The method gets the object with the database fields a...

Assembly.ReflectionOnlyLoadFrom not working

Hello guys, I have an Assembly Library1.dll which contains some Interfaces, which were serialized as a byte array into the database. For some reasons we have to change the Interface properties and defintion. so now i am writing a migration utility. So i have 2 versions of Library1.dll , In my utility i have created a folder where i stor...

Cannot reflect over assembly that shares a dependency of different version

Here's the scenario using Assembly.ReflectionOnlyLoadFrom: Both my assembly Inspected and my reflection Application Inspector reference Assembly Dependency. If Inspector references Dependency 1.0.0.0 and Inspected references Dependency 1.1.0.0, Inspector cannot reflect over any types or methods in Inspected that use a type from Depende...

Using an XPath-style Query with Reflection

I have a class tree, which contains multiple objects in a hierarchy. So I might have a Container object, which hosts 3 SubContainer objects, which in turn host an arbitrary number of Item objects. Is there any way that I can use an XPath-style expression over this object tree, which would imply reflection, so that I could query a proper...

Unity performance considerations with container controlled lifetime - Is there any reflection lag with multiple Resolve<T>() calls?

Is there any reflection performance considerations when repeatedly calling container.Resolve<T>() when a resolution has already been established? I'm using it in an MVC controller to resolve my data service, so it will be called on every HTTP request. I'm storing the container instance in Application state, and I'm using container contr...

How to get a custom attribute from object instance in C#

Let's say I have a class called Test with one property called Title with a custom attribute: public class Test { [DatabaseField("title")] public string Title { get; set; } } And an extension method called DbField. I am wondering if getting a custom attribute from an object instance is even possible in c#. Test t = new Test();...

Is it possible to set this static private member of a static class with reflection?

I have a static class with static private readonly member that's set via the class's static constructor. Below is a simplified example. public static class MyClass { private static readonly string m_myField; static MyClass() { // logic to determine and set m_myField; } public static string MyField { ...

Objective-C class -> string like: [NSArray className] -> @"NSArray"

I am trying to get a string name of a class from the class object itself. // For instance [NSArray className]; // @"NSArray" I have found object_getClassName(id obj) but that requires an instance be passed to it, and in my case that is needless work. So how can I get a string from a class object, and not an instance? ...