reflection

Detect Class from Interface Type

I have an interface, and some classes that inherit from it. public interface IFoo {} public class Bar : IFoo {} public class Baz : IFoo {} If I get the types which implement IFoo, how can I decide if the type will represent a Bar or a Baz (without actually creating the object)? // Get all types in assembly. Type[] theTypes = asm.G...

C# reflection and inheritance of static members

Let's say we have these two classes: public class Base { public static int GetInt() { return 1; } } public class Derived : Base { } Let's also say that a piece of code calls Derived.GetInt(). How can I tell from within GetInt() that it was Derived.GetInt() and not Base.GetInt() that was called? What reflection technique do I use...

Get information about the decorated member by an attribute in C#

I need to know if there's any way (or another distinct approach) to an attribute knows something about what is being decorated for him. For example: class Cat { public Cat() { } [MyAttribute] public House House { get; set; } } Inside MyAttribute I must do some preprocessing with the house object... class MyAttribute : At...

C# "is" operator - is that reflection?

A colleague asked me an interesting question today - is the C# keyword/operator "is" considered reflection? object tmp = "a string"; if(tmp is String) { } How is this operator implemented behind the scenes? Does it require reflection or introspection? Or because of the strongly typed nature of the language, is the Type of the object ...

How to call the construct of a ReflectionObject and how to get this from a ReflectionClass

I have an array of reflectionClasses. I need to get a reflectionObject from one of these and then call its constructor with some parameters. The point is to instantiate an object without knowing the class name (i'll know it at runtime). Example, just to render the idea: foreach (Conf::get_array() as $reflection_class) { //it's n...

Is there a way to go over all types in a namespace in a foreach loop?

Hi, I have a function that receive a type and returns true or false. I need to find out what are all types in a certain namespace that that function will return true for them. Thanks. ...

VB.NET: Optimize initialisation of multiple classes

In a project of mine I'm currently doing this: addTemplateToList(New docNULL) addTemplateToList(New docAgenda) addTemplateToList(New docAgendaNew) addTemplateToList(New docOffer) : 20 more : addTemplateToList(New docRequest) all classes inherit docMain and addTemplateToList( X ) adds x to a List(Of do...

C# - Correct Way to Load Assembly, Find Class and Call Run() Method

Sample console program. class Program { static void Main(string[] args) { // ... code to build dll ... not written yet ... Assembly assembly = Assembly.LoadFile(@"C:\dyn.dll"); // don't know what or how to cast here // looking for a better way to do next 3 lines IRunnable r = assembly.Crea...

How to get the assembly (System.Reflection.Assembly) for a given type in .Net?

In .Net, given a type name, is there a method that tells me in which assembly (instance of System.Reflection.Assembly) that type is defined? I assume that my project already has a reference to that assembly, just need to know which one it is. ...

How to pass the PropertyInfo of a property as parameter in a attribute?

Lets say I have two classes: class A { [SortOrder(3)] public string Name { get; set; } } class B : A { [SortBefore(*****)] public string Age { get; set; } } Note the stars in the property attribute in the second class. Would it somehow (using expressions I guess) be possible to specify A.Name in the SortBefore attribute? ...

Getting type arguments of generic interfaces that a class implements

I have a generic interface, say IGeneric. For a given type, I want to find the generic arguments which a class imlements via IGeneric. It is more clear in this example: Class MyClass : IGeneric<Employee>, IGeneric<Company>, IDontWantThis<EvilType> { ... } Type t = typeof(MyClass); Type[] typeArgs = GetTypeArgsOfInterfacesOf(t); // At...

Log all MethodCalls with minimal code changes (C# 3.0, .NET 3.5)

Hi, I have an application I need to analyze. I have the source code here. I already found a way to log the method calls using this code: Inside Method: logger.MethodTraceLog(); public void MethodTraceLog() { var msg = "MethodTraceLog: " + this.log.Logger.Name + " ### " + new StackFr...

help with reflection + constructors

I have code I'm working on to instantiate a CRC algorithm dependent on a polynomial passed in, and a string s that contains "crc8" or "crc16" or "crc32". The classes CRC8, CRC16, and CRC32 all extend a class CRC and implement an interface HashAlgorithm. Each of them has a constructor CRCx(int polynomial). My problem is, I get this erro...

MVC reusable propertygrid

In my web application framework (currently WebForms) I have a control that behaves like a classic propertygrid. It is initialized with an object ID (database key), then it reads metadata to determine the type of the object and the attributes of the object. It displays the attributes, string attributes as textboxes, bool attributes as che...

Java: Any simple way a function can return the calling class and method name?

Often I will send debugging info to my log file from within Java, and it helps to give decent information as to where in the code the log message was generated so I can quickly refer back to it. I don't want to hard-code anything in the log message about its location, because I have to remember to update the log message if I rename its ...

How to dynamically create generic C# object using reflection?

In C# I have the following object: public class Item { } public class Task<T> { } public class TaskA<T> : Task<T> { } public class TaskB<T> : Task<T> { } I want to dynamically create TaskA or TaskB using C# reflection (Activator.CreateInstance). However I wouldn't know the type before hand, so I need to dynamically create TaskA bas...

Can i store serializable array data in DataColumn?

I am trying to automatically convert object's properties to DataTable (object is array and has properties that instantiated from special class which has value type). The code: static public DataTable f_GetDataTableFromClassObject(object _objInstance) { // geri dönecek datatable DataTable dataTable = new DataTable(); // ne...

Using reflection with generic types and implicit conversions

I'm trying to use reflection to set properties on some OpenXML types (e.g. Justification). Assigning a value by enumerating all possibilities is straight-forward: // attr is an XmlAttribute, so .Name and .Value are Strings if (attr.Name == "Val") { if (element is Justification) { ((Justification)element).Val = (Justific...

C# GetType().GetFields problem

Hi I am using the Reflection classes in order to get all the fields inside a certain object. My problem however is that it works perfectly when the fields are inside a normal class, like: class test { string test1 = string.Empty; string test2 = string.Empty; } Here i get both test1 and test2, my problem is that i use abstractio...

Ignoring Version in an assembly-qualified name passed to Type.GetType()

Is it possible to get a Type via Type.GetType() when the assembly-qualified name passed into GetType() specifies a different Version than the version of the DLL that's actually loaded? If so, what is the behavior of GetType()? I want to get a Type from an assembly regardless of what version the assembly is. I have a function which gets ...