reflection

Using reflection to address a Linqed property

I'm trying to writing a generic method that will load a record of a specific type, with a specific ID. Here's one way that works: public abstract class LinqedTable<T> where T : LinqableTable { public static T Get(long ID) { DataContext context = LinqUtils.GetDataContext<T>(); var q = from obj in context.GetTable<T>() whe...

C# instantiate/initialize object at program startup

I'm a C/C++ programmer recently working in C#, and i'm trying to do some fancy initialization stuff that I've run into some trouble with. The best and easiest example i can come up with would be that I want to create an "Eager" Singleton - one that is created immediately at program startup, but without me requiring to go into the main f...

c# choosing class properties with a variable?

Hello All, I'm adding functionality to one of our existing (but broken) systems. It grabs an XML file from a web service, parses it and then does some stuff before packing it back into our database. the previous developer (who has now left) left me this little gem: http://dl.getdropbox.com/u/109069/wut.GIF and I wonder if there's a ...

Overhead of searching and calling a method with reflection

I have a class that should read a message, extract its type from the envelope and then call a method that have to process the message; at each message type is associated an enum member. There are 30+ message types. One way to dispatch the message is to just use a switch, but it's really ugly and error-prone (have I already tract that c...

How do I check if a given value is a generic list?

public bool IsList(object value) { Type type = value.GetType(); // Check if type is a generic list of any type } What's the best way to check if the given object is a list, or can be cast to a list? ...

Extracting Property Names For Reflection, with Intellisense and Compile-Time Checking

Ok. So I have some code that maps certain controls on a winForm to certain properties in an object, in order to do certain things to the controls when certain things happen to the data. All well and good, works fine. Not the problem. The issue is, to add items to the mapping, I call a function that looks like: this.AddMapping(this.m...

Performance issues with using reflection

Duplicate How slow is Reflection (C#) What are some of the performance considerations when using reflection to iterate through properties and using attributes on properties in c#.net? ...

.NET Reflection Create Class Properties

Hi, I am fairly new to reflection and I would like to know, if possible, how to create an instance of a class then add properties to the class, set those properties, then read them later. I don't have any code as i don't even know how to start going about this. C# or VB is fine. Thank You EDIT: (to elaborate) My system has a dynamic ...

Dynamically invoking any function by passing function name as string

Hello, How do I automate the process of getting an instance created and its function executed dynamically? Thanks Edit: Need an option to pass parameters too. Thanks ...

Problem reflecting in ASP.net context

I have a ASP.net application that is referencing a external assembly that I need to dynamically load and discover any types implementing a known interface. The problem I am having is that the type I reflect does not match the same interface that is running and so I cannot cast it. Example: This code is run in ASP.net app. var assembl...

Deriving Class from Generic T

I have a parameterized hibernate dao that performs basic crud operations, and when parameterized is used as a delegate to fulfil basic crud operations for a given dao. public class HibernateDao <T, ID extends Serializable> implements GenericDao<T, ID> I want to be able to derive Class from T at runtime to create criteria queries in Hi...

C# Reflected Property Type

I am using reflection to get the values out of an anonymous type: object value = property.GetValue(item, null); when the underlying value is a nullable type (T?), how can I get the underlying type when the value is null? Given int? i = null; type = FunctionX(i); type == typeof(int); // true Looking for FunctionX(). Hope this make...

C# Reflection : Finding Attributes on a Member Field

I may be asking this incorrectly, but can/how can you find fields on a class within itself... for example... public class HtmlPart { public void Render() { //this.GetType().GetCustomAttributes(typeof(OptionalAttribute), false); } } public class HtmlForm { private HtmlPart _FirstPart = new HtmlPart(); [Optional] //<-- ...

How can I get the assembly last modified date?

I want to render (for internal debugging/info) the last modified date of an assembly, so I know when was a certain website deployed. Is it possible to get it though reflection? I get the version like this, I'm looking for something similar: Assembly.GetExecutingAssembly().GetName().Version.ToString(); ie: I don't want to open the ph...

A Job For Reflection?

Greetings! I have a class which is used like a cache: public sealed class MyCache<T> : IDisposable { private ReaderWriterLockSlim theLock = new ReaderWriterLockSlim(); private Dictionary<int, T> theCache = new Dictionary<int, T>(); public void Add(int key, T value) { // ... logic/code to add to the dictionary ...

Reflection: for frameworks only?

Somebody that I work with and respect once remarked to me that there shouldn't be any need for the use of reflection in application code and that it should only be used in frameworks. He was speaking from a J2EE background and my professional experience of that platform does generally bear that out; although I have written reflective app...

Nested enums with System.Reflection.Emit

I want to create a class with a nested enum. public class Foo { public enum Views { } } However System.Reflection.Emit.TypeBuilder class has no DefineNestedEnum only DefinedNestedType. ModuleBuilder.DefineEnum exists that let's me create an enum but I find no way to make it nested. Can I create an enum without faking it ...

Nested classes and recursion

Say i have these two classes public class Container { public string name { get; set; } public Inner Inner { get; set; } } public class Inner { public string text { get; set; } public Inner2 Innert2 { get; set; } } public class Inner2 {} How would i go, given an instance of ...

c#, Internal, and Reflection

Duplicate of: Accessing internal members via System.Reflection? Is there a way to execute "internal" code via reflection? Here is an example program: using System; using System.Reflection; namespace ReflectionInternalTest { class Program { static void Main(string[] args) { Assembly asm = Assembly...

Loading types from .NET executables

Is there any implications of loading types dynamically from a .NET assembly that is executable (.exe) compared to loading types from an .NET assembly compiled into a .dll? What is the best and quickest way to test .exe and .dll if it is a .NET executable or not (just not a big fan of the BadImageFormatException)? Thank you. ...