reflection

C# : Is there a generic way to forward a method call to another object (with the same interface)?

Is there a way to implement this pattern in a generic way? A dispatcher object and a bunch of worker objects all derive from the same interface. Any method call into the dispatcher object needs to be dispatched (forwarded) to one of the worker objects (with all the arguments). Each method would need to discover it's own name, find the...

Convert B(C(),D()) to (c,d)=>B(()=>c(),()=>d()) - Generating delegate wrappers from MethodInfos?

Ultimately, I'm looking forward to a reflection based approach to create a delegate wrapper for a method B(C(),D()) - to some what like (c,d)=>B(()=>c(),()=>d()) First question - Given that you've a Methodinfo, what are the considerations to create a delegate type (via reflection) which corresponds to that method signature? Do you hav...

TypeDescriptor doesn't return members from inherited interfaces

my problem is that TypeDescriptor doesn't return members from inherited interfaces, is this how it is supposed to be working ? or is it a bug ? [TestFixture] public class DescriptorTests { [Test] public void Test() { // count = 1 ...

GetMethod for generic method

I'm trying to retrieve MethodInfo for Where method of Enumerable type: typeof (Enumerable).GetMethod("Where", new Type[] { typeof(IEnumerable<>), typeof(Func<,>) }) but get null. What am I doing wrong? ...

How can I use reflection to convert from int to decimal?

I have some code (which works fine) that looks something like this: int integer = 42; decimal? castTo = integer; Then I wanted to do something similar with reflection, with some code that looks like this: object value = source; // source was an int originally var parameters = new object[1]; ... parameters[...

How to pass a C# Select () extension delegate or expression to a Repository?

Everyone projects once in a while: var c = dc.Products.Select( p => new {p.id, p.name}); // results in c having properties of .id and .name What if I want to refactor that in to a method. How would I pass the Select parameter? var c = myFunction( p => new {p.id, p.name}); Product myFunction( ??? myLambda) { var c = dc.Product...

C# GetType(): Code how to get the type of T in List<T> without actual elements?

If I do something like this: var a = new List<something>(); var b = new something(); a.Add(b); var c = a[0].GetType(); C holds the type I want (which is "something"). How could you get the type of "something" without creating the list? ...

C#: Given src => src.property, code choosing property if you have a string "propertyname"?

I am using AutoMapper where it has: .ForMember( dest => dest.id, opt => opt.MapFrom(src => src.id)) Using the far right expression src => src.id, if I have a string variable with the property's name, how would I choose the property by it? I tried this: src => propertyName Then had to laugh when the value was "id". ...

What C#/Linq code copies all matching property name values between two objects without knowing their type?

I have a well known POCO class of Customer to return from my method. However, I only populate properties specified by an ever changing Expression p => new {p.id, p.name} for example, as a parameter to the method. Somehow I need to copy all matching fields between these two objects. var returnObject = IList<Customer>(); var partialField...

Unity Container Type Registration Quirk

Hi All, I'm trying to automatically register all reports in a unity container. All reports implement IReport and also have a Report() attribute which defines the title, description and unique key (so I can read these without instantiating a concrete class). So... I get the report types like this Public Shared Function GetClassesWhic...

How to use reflection on a property of type ICollection?

I have a method where I am passing in two object, which have the same property names, and I'm using Reflection to get the values from one object and set the values on the other. My problem is when I come across a property that is a collection, the original is EntityCollection and the one getting set is ObservableCollection, and I'm obvio...

Given a Generic <E> how to create a new E and return it?

This code returns a variable set of fields and I want to return a strongly typed <E>: public IList<E> Get(Expression<Func<E, object>> selectLambda == null) { if (selectLambda == null) selectLambda = p => p; var partialSet = DC.CreateQuery<E>("[" + typeof(E).Name + "]"); foreach ( var record in partialSet) { ...

.net Reflector "Object reference not set to an instance of an object"

Hello, I'm working with .net Reflector to view some .net Compact Framework .dlls and I'm getting an "Object reference not set to an instance of an object" error for one of them. I downloaded the trial version of 9Rays.Spices.Net and it loaded the .dll just fine. The only problem is that I'm getting only every other method. I was able...

C# Create a Delegate that fires an event?

Is it possible to use Reflection is C# to fire an event? Say I have a situation like this: public delegate void SomeEventHandler(object sender, BenArgs e); class EventHub { public event SomeEventHandler SOME_EVENT; public void fireEvent(String eventName) { SomeEventHandler evt = (SomeEventHandler) Delegate.CreateDe...

Get the outer class object from an inner class object

In a nutshell, I'm trying to do the inverse of "classObject.getDeclaredClasses()". I have a method that receives an object of type Class<? extends Object>. I want to figure out whether it is an inner class, and if it is, I want to access the surrounding class' object instance. Is there a smart API for this, or am I forced to do some st...

Does `productElement(i)` on a case-class use reflection?

Considering the following Scala snippet: case class Foo(v1: String, v2: Int, v3: Any) def inspect(p: Product) = (0 until p.productArity).foreach(i => println(p.productElement(i))) inspect(Foo("Moin", 77, null)) Does the invocation of inspect() here means that reflection is used (in whatever way)? I'd like to somehow be able to ac...

How to get the actual property name when referencing an interface?

Consider the following case: Public Interface IHasDateUpdated Property DateUpdated As DateTime End Interface Public Class MyClass Implements IHasDateUpdated Public Property MyDateUpdated As DateTime Implements IHasDateUpdated.DateUpdated End Class Now, assuming I was referencing an instance of MyClass as an IHasDateUp...

How To Use Reflection To Find UserControls On A Page In A Nested MasterPage

I have a CommonMaster page that then contains a ConfigurableReportsMaster page. I then have an AutitLog.aspx page. Now, on that .aspx page I have multiple UserControls. Some of these UserControls are inheriting from an ISQLFilter interface, but not all. While on the .aspx, in a RunReport event (that currently successfully fires) I ...

how to automatically copy values from java bean to protobuf message object using java reflection ?

Typically I could copy values between two java beans , which have identical property names, using beanutils with java reflection e.g. PropertyUtils.setProperty(....) In protobuf Message, we use the message builder class to set the value. This works but I would rather use reflection to automatically copy properties from the bean to the...

Representration of a Empty Class Array

Hi Guys, I am actually looking at getting the method, whose name is stored in the string "methodName", from the Class "CC1" using Java Reflection. Method actualMethod= CC1.getMethod(methodName, parameterTypes); This is the syntax. The problem is the method takes no parameter. How do I represent that in the parameterTypes ? where p...