reflection

Set InnerException of .NET Exception object

How can I set the InnerException property of an Exception object, while I'm in the constructor of that object? This boils down to finding and setting the backing field of a property that has no setter. If your answer is this cannot be done, don't answer please! I'm saying this because it should be possible via reflection, so if you kn...

I'm currently using reflection to do this, is there a better way?

o will always come from an entiryRef/TEntity (from linq 2 sql) I'm ok with a c# or vb.net solution (I will convert it into vb.net if you can't) Public Function desc(Of t)(ByRef o As t, Optional ByVal PropPrefix As String = "desc") As String If o Is Nothing Then Return "" Else Dim bind = Reflection.BindingFlags.P...

ASP.NET MVC Issue with Using Reflection Created Objects with the Default Model Binder

I am having a weird issue in ASP.NET MVC with objects not being updated with UpdateModel when passed a formCollection. UpdateModel does not appear to be working properly when the object being updated is created through reflection. Scenario: I have an application which has approximately 50 lookup tables--each of which includes exactly th...

Scala: How do I dynamically instantiate an object and invoke a method using reflection?

In Scala, what's the best way to dynamically instantiate an object and invoke a method using reflection? I would like to do Scala-equivalent of the following Java code: Class class = Class.forName("Foo"); Object foo = class.newInstance(); Method method = class.getMethod("hello", null); method.invoke(foo, null); In the above code, bot...

detecting if type implements ICollection<T>

Hello, I am trying to check if a type implements the generic Icollection interface, since this is a base interface for any of my generic collections. the below code doesnt work GetType(ICollection(Of)).IsAssignableFrom( objValue.GetType().GetGenericTypeDefinition()) whats a good way of detecting if a type implements a generic in...

Java Creating a new Class with Generics

I'm running into a problem with my program where given an object and an attribute name I want to return the method's return type. public static Class<?> getAttributeType(Object object, String attributeName) { try { Method method = object.getClass().getMethod( "get" + StringUtils.capitalize(attributeName)); ...

VB.Net Running Threading with Reflected Objects

Running into problems creating objects through reflection and then running them on multiple threads. I just cannot seem to figure out what I need to here: For Each WorkerNode As XmlNode In oRunSettings.GetWorkerValues Dim sWorkerName = WorkerNode.Attributes(SETTING_NAME_ID).Value Dim oWorkerT...

Extension method using Reflection to Sort

I implemented an extension "MyExtensionSortMethod" to sort collections (IEnumerate). This allows me to replace code such as 'entities.OrderBy( ... ).ThenByDescending( ...)' by 'entities.MyExtensionSortMethod()' (no parameter as well). Here is a sample of implementation: //test function function Test(IEnumerable<ClassA> entitiesA,IEnum...

Is it possible to see if a class calls a method without first instantiating it?

I'd like to know if a class is going to call a certain method, but I want to know before instantiating the class. Is this possible? Example: class Controller_Index calls $this->composite('SomeCompositeClass') within its __construct() method. Class SomeCompositeClass has a helloWorld() method. I want to see if I can call Controller_Inde...

Why does the runtime shows the generic types as "GenericType`n"?

And why doesn't it shows the real type (eg: List<string> instead of List`1)? Where does this strange (to me) notation comes from? ...

Resources for Reflection and Attributes usage

Are there any good resources out there that explain attributes and reflection and their many uses in depth? I am especially interested in best practices, i.e. when it would be useful or appropriate to use attributes and reflection to solve specific computing problems. ...

C# - Get values of static properties from static class

I'm trying to loop through some static properties in a simple static class in order to populate a combo box with their values, but am having difficulties. Here is the simple class: public static MyStaticClass() { public static string property1 = "NumberOne"; public static string property2 = "NumberTwo"; public static string...

Set property Nullable<> by reflection

I try to set a Nullable<> property dynamicly. I Get my property ex : PropertyInfo property = class.GetProperty("PropertyName"); // My property is Nullable<> at this time So the type could be a string or int I want to set my property by reflection like property.SetValue(class,"1256",null); It's not working when my property is a Nu...

Reflection C# question

I am having problem getting values of a class whose one property is another class. Here is an example: public class Person { private int age; private string name; public Person() { Address = new Address(); } public int Age { get { return age; } set { age = value; } } publ...

Why can't I dynamically construct a System.Xml.XmlDocument?

The following code fails with a 'Could not load type 'System.Xml.XmlDocument' from assembly' error: object a = Type.GetType("System.Xml.XmlDocument", true); I have a reference to System.Xml in which XmlDocument resides. Any idea what I am doing wrong? ...

C# 2.0 Dynamic Event Subscription with known event signature

Hi, Similarly to this question: http://stackoverflow.com/questions/45779/c-dynamic-event-subscription I would like to be able to wire up an event handler to an event fired out of a dynamically-created object. I am doing this to verify that my JavaScript and other non-.NET code is able to connect to the objects' events. My event signa...

Why can you reflect and call a (not so) private method in Java and .Net

In both Java and C# it is possible to invoke a private method via reflection (as shown below). Why is this allowed? What are the ramifications of doing this? Should it be taken away in a future version of the language? Do other languages/platforms allow this?If I have this class in both Java and C# Here is the example public cla...

Efficient way to convert unknown objects to appropriate Method.invoke arguments?

I'm writing a reflection-based RPC service that gets arguments passed in via a variety of mechanisms. Sometimes the arguments correctly match the parameter type, sometimes they're always strings, and sometimes they're wrapped up in dynamically typed "scripty" objects that need the appropriate value extracted out. Before I can call meth...

ReflectionClass::getProperty for a private property in an inhertited class

Suppose you have a class A with a private property $a, and a class B which inherits from that. If I use ReflectionClass::getProperty on an instance of B, I get a fatal error, that the property doesn't exists. This makes sense, as it is it a private method. However, ReflectionClass::hasProperty returns true, nonetheless. Is this a bug? ...

java: using a variable's value as an object name (not the eval() way)

Ok, so coming from a background of mostly perl, and mostly writing dirty little apps to automate my tasks, I've read the pages about the evils of eval(), and I always use a hash (in perl). I'm currently working on a little project (mostly for me and a couple of other technical people at work), for creating "canned response" e-mails. To a...