reflection

Activator.CreateInstance<T> Vs new

Is there any difference between following two ways of creating an object. Student s1 = Activator.CreateInstance<Student>(); Student s1 = new Student(); Is there any difference in , the way constructor is called ? initializing the memory ? As per my understanding, the first method looks completely redundant to me. If programmer knows ...

Resolve class name from bytecode

Is it possible to dig up a classes name from bytecode which is formed from the class' source code? The situation is this: I get a classes bytecode remotely from somewhere, it doesn't matter where it comes from. To effectively load that class with a classloader i would need to have the class name as well... right? ...

.NET Reflection: Detecting IEnumerable<T>

I'm trying to detect if a particular instance of a Type object is a generic "IEnumerable"... The best I can come up with is: // theType might be typeof(IEnumerable<string>) for example... or it might not bool isGenericEnumerable = theType.GetGenericTypeDefinition() == typeof(IEnumerable<object>).GetGenericTypeDefinition() if(isGenericE...

Converting Reflected Property to String

I've got a problem converting an object property to string while using reflection... string value = Convert.ToString(typeof(T).GetProperty(ValueField).GetValue(data, null)); This throws 'Object does not match target type.' when returning any type other than string? ...

StackTrace filename unknown

Something strange is happening in my code where I'm using a StackTrace. It's almost as if the debug info is not being loaded... but I'm running this on the DEBUG build.The .pdb files are definitelly in the bin directory and up to date. I've seriously ran out of ideeas : public class TraceHelper { private static IDictionary<string,i...

.NET reflection: determining whether an array of T would be convertible to some other type

Note, this question is a bit subtle so read it carefully: I'm not just trying to find out whether some artibitrary type implements IEnumerable: Here's a function I've written with an initial implementation: // is "toType" some sort of sequence that would be satisfied // by an array of type T? If so, what is the type of T? /...

Using Reflection.Emit to create a class implementing an interface

I need to generate a class using Reflection.Emit that implements the following interface. public interface IObject { T Get<T>(string propertyName); } Does anyone have an example of how I would emit the following as a simple test case? class GeneratedObject : IObject { public T Get<T>(string propertyName) { // thi...

How to fetch object name using reflection in .net?

In .net how do I fetch object's name in the declaring type. For example... public static void Main() { Information dataInformation = new Information(); } public class Inforamtion { //Constructor public Inforamtion() { //Can I fetch name of object i.e. "dataInformation" declared in Main function //I want to set the object's Name prop...

Java reflection: How can I retrieve anonymous inner classes?

Hello, everyone! I have an anonymous inner class inside another class (SomeClass). Both SomeClass.class.getClasses() and SomeClass.class.getDeclaredClasses() return empty arrays. I couldn't find some hints on this in Class' Javadocs. Can anonymous inner classes be retrieved using reflection in some way? What else are notable differe...

C# Attribute to modify methods

Hello, all. Maybe i've not googled enough, but i can't find any example on this question. It is possible in C# to create an custom attribute which, applied to a class, modifies all of its methods? For example, adds Console.WriteLine("Hello, i'm modified method"); as a first line (or it's IL equivalent if it's done at runtime)? ...

typeof(System.Enum).IsClass == false

Founded that: typeof(System.Enum).IsClass == false It's become strange that System.Enum has also .IsValueType == false, but Reflector shows that it is really just an abstract class. System.Enum is a reference type like a System.ValueType and casting enumeration values to/from System.Enum reference caused boxing/unboxing. No surprises...

Can an array be assigned to an array of an unknown type when you know the name of the field to assign to?

Hey guys, I need to assign an array to a field. I dont know the fields type, but I do have a reference to an instance and the name of the field. I can assume the array can be casted to the fields type. Can this be done? Bas Edit: Hopefully this code will clarify what Im trying to do, this causes an exception in assign: class MyClass...

Dynamic method return types in java

The following code does exactly what I want it to, but I am curious if there is a better way of going about it. This would be so much easier if Interfaces allowed static methods, or if Java methods could be generalized/parameterized to the extent they can in C#. I would much rather substitute the parameter "Class<TParsedClass> c" for "C...

How can I get all the inherited classes of a base class?

class Foo { } class Foo1 : Foo { } class Foo2 : Foo { } How would I be able to get all the classes that use Foo as a base class? The inherited classes aren't necessary in the same assembly. ...

C# - Casting an 'object' parameter into that object's type?

C# Hi all, I pass an object to a method. I want to cast that object to its specific class so I can perform its own specific methods? How can I do that? Move( new Cat() ); Move( new Pigeon() ); public void Move(object objectToMove) { if(objectToMove== Cat) { Cat catObject = objectToMove as Cat; catObject.Walk(...

[C#] Assembly instance with no types in it

How do I get Assembly reference for a .dll with no types in it? var assembly = typeof (SomeType).Assembly; --> but without having to spacify any type? Something like Assembly.Get("AssemblyName"); ...

Copy all values from fields in one class to another through reflection

Hi. I have a class which is basically a copy of another class. public class A { int a; String b; } public class CopyA { int a; String b; } What I am doing is putting values from class A into CopyA before sending CopyA through a webservice call. Now I would like to create a reflection-method that basically copies all fields t...

Cast down with reflection at runtime

Considering following code public class A { public static void main(String[] args) { new A().main(); } void main() { B b = new B(); Object x = getClass().cast(b); test(x); } void test(Object x) { System.err.println(x.getClass()); } class B extends A { } } ...

How do you get a C# property name as a string with reflection?

I'm looking for a way to get a property name as a string so I can have a "strongly-typed" magic string. What I need to do is something like MyClass.SomeProperty.GetName() that would return "SomeProperty". Is this possible in C#? ...

Compiled dynamic language

Greetings, I search for a programming language for which a compiler exists and that supports self modifying code. I’ve heared that Lisp supports these features, but I was wondering if there is a more C/C++/D-Like language with these features. To clarify what I mean: I want to be able to have in some way access to the programms code at...