reflection

Create List<T> using GetType()

Is it possible to create a List or IEnumerable using GetType(). // If T is Type of Contact I want to Return List<Contact> Test(typeof(Contact));//return List<Type> public static IEnumerable<T> Test<T>(T t) { return new List<T>(); //return List<Type> } ...

Reflection on method parameters in Ruby

Take the following class: class Automator def fill_specific_form(fields) fields.each_pair do |key, value| puts "Setting '#{key}' to '#{value}'" end end end a = Automator.new a.fill_specific_form :first_name => "Mads", :last_name => "Mobæk" # => Setting 'first_name' to 'Mads' # => Setting 'last_name' to 'Mobæk' Is i...

How to use .NET reflection to determine method return type (including void) and parameters?

how to know the number and type of parameters? how to know the return type? how to check whether the return type is void? ...

Setting properties with reflection on static classes

I want to make a static class that would load some settings from XML file and apply those settings to its own properties. I am trying to use the following code but I don't really know what to give to the SetValue method since the class for which we want to set the property is static. // some code removed ... Type settingsType = typeof(...

Reflection PropertyInfo.GetValue from Collection

I have problem with reflection, dynamic invoking objects and reading collection values. In Referenced COM/Interop it would look like this: ICollection collection = (ICollection)sth.getCollection("parameter"); SomeObject obj = (SomeObject)collection["id='1'"]; //DB WHERE condition Unfortunetly i need to make it with reflection and dyna...

Lazy<T> implementation and .NET generics

I was looking for ways to do lazy initialization and found Lazy<T> which is included in .NET 4. I was thinking of rolling my own implementation of Lazy<T> for .NET 3.5 (with a simpler multi-thread policy), and I bumped into the following problem: Lazy has basically two types of constructors: class Lazy<T> { public Lazy(){...} // ...

If given the task to write hello world in C#... using reflection, what are some clever ways to use reflection?

I've been given a "for fun" challenge to write a "hello world" application. The only condition is that I use reflection, there is no specified way this condition must be met, but clever or elegant or "cool" uses are fun. Aside from injecting an external library or doing some sort of IHelloWorldMessageProvider composition container mojo...

Get Method Details using reflection and decorated attribute

Hey There I have a custom attribute : public class MenuItemAttribute : Attribute { } and i have a class with a few methods: public class HelloWorld { [MenuItemAttribute] public void Shout() { } [MenuItemAttribute] public void Cry() { } public void RunLikeHell() { } } Using reflection, ...

C# OO design problem with override from methods

Situation: Assembly 1 ________________________ ________________________ | Class A | | Class B | |-----------------------| |-----------------------| | Method someMethod |---------->| Method otherMethod | | | | | |_____...

What is BindingFlags.InvokeMethod meant for?

I have the following code used to avoid a switch statement to decide which method to invoke, and it works with just the BindingFlags flags I have set, without InvokeMethod. What is InvokeMethod actually meant for and why is it not needed in the following code: public enum PublishMethods { Method1, Method2, Method3 } private...

get type IRepo<T> when i have the string name for T

basically I have string s = "Foo" I need to obtain Type t = IRepo<Foo> but from string ...

Java: Convert Primitive Class

Hi is there an easy in Java to convert primitive class objects into object class objects? Given a class Class cl, I want to convert it into a Class that has no primitives. Eg. Class<?> cl = int.class; ... if (cl.isPrimitive()) { cl = Object of primitive } ... cl == Integer.class I would like a method that does that for all primit...

C# Reflection: GetProperties for 'method-like' read-only properties

UPDATE: Sorry, I was wrong, StringBar is returned. Let's see if I can work out how to delete this question. Imagine the following class: public class Foo { public int Bar { get; set; } public string StringBar { get { return Bar.ToString(); } } public void DoSomething() { } } If I write: typeof(Fo...

Classpaths and reflection

In a project that makes heavy use of reflection to select classes based on environment, is there a way to determine what libraries to include in the build? At the moment, taking a library out of the project is a gamble, but sticking with the current plan of "include everything, it's the only way to be sure" makes the whole thing bloated...

Is it possible to programmatically check when a .NET control will set off UAC?

I'm trying to use reflection to have a program look at itself and determine whether a certain control triggers UAC. I'd either like to be able to see that the event handler on the button creates a privileged process or to check whether a given button has the UAC shield enabled on it. Is this possible? ...

How to dynamically set a property of a class without using reflection (with dynamic) in C# 4 when property name is coming from another source

Hi, I'm building/updating an EntityFramework EntityObject on runtime. I want to set the properties of the entity class, property names and values are coming from another source. So I'm doing this; public static EntityCollection<T> UpdateLocaleEntity<T>(EntityCollection<T> entityCollectionToUpdate, params ILocaleControl[] values) ...

ASp.NET MVC 2 - find a view embeded in the compiled assembly

Hey There I have an MVC Application the views was embedded in the compiled dll, how can i get a list of all the views? I am using reflection to get all the controller info, however, i do not know the view info. ...

Range DataAnnotation Doesn't Seem to Be Working in .Net 3.5

Using .Net 3.5 I have a Range Attributes (System.ComponentModel.DataAnnotations) on a Property... [Range(0, 5, ErrorMessage = "Weight must be between 0 and 5")] public virtual double Weight{ get; set; } And I have a Validate method in the class that checks validation attributes... protected virtual void Validate() { var t...

Determining a Generic Type at Runtime in Non-Generic Class

I've gotten myself in a pickle, and could use the help of a guru... I have a Journal that records entries for different types: Journal(Of ParentT) - Parent could be Customer, Address, other classes The constructor of the Journal requires knowledge of the Type parameter: Public Sub New(Parent as ParentT) In my consuming form, I take ...

Is there any way to get the PropertyInfo from the getter of that property?

Is there any way I can get the PropertyInfo for a property from its getter? Like this: public object Foo { get { PropertyInfo propertyInfoForFoo = xxx; ... } } I want to avoid having to hard code the name of the property as a string, as that's tricky to maintain. I'm using .NET 2.0, so I'm hoping for a lin...