reflection

How to use reflection to call generic Method?

What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the Example() method, what's the most concise way to invoke GenericMethod() using the type stored in the myType variable? public class Sample { ...

Generics and Type inference

I have an abstract generic class BLL<T> where T : BusinessObject. I need to open an assembly that contains a set of concrete BLL classes, and return the tuples (businessObjectType, concreteBLLType) inside a Dictionary. There is the part of the method I could do until now, but I'm having problems to discover T. protected override Diction...

Add property to anonymous type after creation

I use an anonymous object to pass my Html Attributes to some helper methods. If the consumer didn't add an ID attribute, I want to add it in my helper method. How can I add an attribute to this anonymous object? ...

BindingFlags for Type.GetMethods excluding property accesors

Suppose I've got the following program: namespace ReflectionTest { public class Example { private string field; public void MethodOne() { } public void MethodTwo() { } public string Property { get { return field; } set { this.field = value; } } } ...

How do I get the value of MemberInfo?

How do I get the value of a MemberInfo object? .Name returns the name of the variable, but I need the value. I think you can do this with FieldInfo but I don't have a snippet, if you know how to do this can you provide a snippet?? Thanks! ...

Given a type ExpressionType.MemberAccess, how do i get the field value?

I am parsing an Expression Tree. Given a NodeType of ExpressionType.MemberAccess, how do I get the value of that Field? From C# MSDN docs: MemberAccess is A node that represents reading from a field or property. A code snippet would be incredibly, incredibly helpful. Thanks in advance!!! My code looks something like this: public ...

Dynamically invoke properties by string name using VB.NET

I'm currently working on a project where a section of the code looks like this: Select Case oReader.Name Case "NameExample1" Me.Elements.NameExample1.Value = oReader.ReadString .... Case "NameExampleN" Me.Elements.NameExampleN.Value = oReader.ReadString .... End Select It continues on for a while. The c...

How do you get the all properties of a class and its base classes (up the hierarchy) with Reflection? (C#)

So what I have right now is something like this: PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public); where obj is some object. The problem is some of the properties I want aren't in obj.GetType() they're in one of the base classes further up. If I stop the debugger and look at obj, the I have to dig through a few...

How do I get the properties of an object using reflection?

I know I can do this foreach (PropertyInfo property in myobject.GetType().GetProperties()) { if (property.DeclaringType.ToString() == myobject.GetType().ToString()) { // only have my object properties here // and not parent of my object properties } } But how can I just get the properties of myobject and...

Is it possible to modify a method body at run-time in .NET?

I know that it is possible (in theory) to create a new type at run-time, but is it possible to modify a method body of an existing type at run-time? My plan (if I can get this to work) is to tag up methods with a custom attribute, and then at run-time search for methods with the attribute, and insert some of my own code into the method b...

is something similar to ServiceLoader in Java 1.5?

Hello i want to discover at runtime classes in the classpath which implements a defined interface. ServiceLoader suits well (i think, i haven't used it), but i need do it in Java 1.5. any ideas? ...

Attributes on an interface

I have a interface that defines some methods with attributes. These attributes need to be accessed from the calling method, but the method I have does not pull the attributes from the interface. What am I missing? public class SomeClass: ISomeInterface { MyAttribute GetAttribute() { StackTrace stackTrace = new StackTra...

Does reflection expose if the last argument for a method was marked with 'params'?

Using reflection on a method definition I would like to find out if the original method was defined with 'params' on the last parameter. So can I discover if the original definition was this... public void MyMethod(int x, params object[] args); ...and not this... public void MyMethod(int x, object[] args); My code has a list of arg...

Reflection optimizations with attributes . .

Is there a way in C# to: Get all the properties of a class that have attributes on them (versus having to loop through all properties and then check if attribute exists. If i want all Public, Internal, and Protected properties but NOT private properties, i can't find a way of doing that. I can only do this: PropertyInfo[] props = ty...

Reflecting local variables

I'm trying to find a way to automate some exception logging code to add to the stack information already available. Is there any way to use reflection to retrieve the values of all variables on the stack (locals and parameters) - I sincerely doubt the names of the variables are available, but in many cases it would be useful to see the ...

List Object's built-in Properties

Is there a way for me to loop over a Javascript Object's built-in properties? for...in gets me close to where I want to go, but "A for...in loop does not iterate over built-in properties." ...

XMLHTTP POST request and System.Reflection

I have a DTS job that is using the MSXML2.XMLHTTP3.0 object to generate a post request to an ASP.NET application. Under the covers, the ASP.NET application is using System.Reflection to acquire some assembly information and I receive the following exception: System.Web.HttpException Error Code: -2147467259 Message Session state ca...

BindingFlags.IgnoreCase not working for Type.GetProperty()?

Imagine the following A type T has a field Company. When executing the following method it works perfectly: Type t = typeof(T); t.GetProperty("Company") Whith the following call I get null though Type t = typeof(T); t.GetProperty("company", BindingFlags.IgnoreCase) Anybody got an idea? ...

How do I add attributes to a method at runtime?

We're using Microsoft.Practices.CompositeUI.EventBroker to handle event subscription and publication in our application. The way that works is that you add an attribute to your event, specifying a topic name, like this: [EventPublication("example", PublicationScope.Global)] public event EventHandler Example; then you add another attr...

Calling a control's Validate() method using Reflection

Hi All, I'm currently in the process of writing a wizard and want to make each page validate before moving onto the next page. I want to prevent the user from progressing by calling the Validate() method on every child control on the page and and stopping navigation if any of them fail. The problem is that the Validate() method on eve...