I have a C# method which creates a new instance of a class from a string, however, I get an error when running the code.
obj = (ClassX)Activator.CreateInstance(Type.GetType("classPrefix_" + className));
ArgumentNullException was unhandled
Value cannot be null
Parameter name: type
Any help on this error would be apprecia...
Imagine I have the following class:
class Cow {
public static bool TryParse(string s, out Cow cow) {
...
}
}
Is it possible to call TryParse via reflection? I know the basics:
var type = typeof(Cow);
var tryParse = type.GetMethod("TryParse");
var toParse = "...";
var result = (bool)tryParse.Invoke(null, /* what are ...
This is probably a very basic question, but I'm really new to generics in Java and I'm having a hard time altering my thought process from the way things are done in C#, so bear with me.
I'm trying to build a generic repository in Java. I've created an IRepository interface that looks like this:
public interface IRepository<T extends I...
I want to access the properties of a class from the attribute class by using reflection. Is it possible?
For example:
class MyAttribute : Attribute
{
private void AccessTargetClass()
{
// Do some operations
}
}
[MyAttribute]
class TargetClass
{
}
...
I was trying to get Matz-n-Flanagan's RPL book's metaprogramming chapter into my head. However I couldn't understand the output from the following code snippet that I dreamed up.
p Module.constants.length # => 88
$snapshot1 = Module.constants
class A
NAME=:abc
$snapshot2 = Module.constants
p $snapshot2.length ...
Hey, long time listener first time caller, and I'm asking a question related to Java reflection, and how easy it lends itself to apparently ugly coding. The following method attempts to take two similar objects (one object having all of the fields of the other object, and then some) and compare the two of them for equality. It will (al...
In all the books I've read on reflection they often say that there aren't many cases where you want to generate IL on the fly, but they don't give any examples of where it does make sense.
After seeing Reflection.Emit as a job requirement for a gaming company I was curious where else it's being used.
I'm now wondering if there are an...
It's just a service locater type of pattern I am trying to implement, where I'd like to catch an attempt to register an implementation to an interface it doesn't belong to, as in:
public void Add(Type interfaceType, object implementingObject)
{
// ... check for nulls
// NO GOOD
if(!implementingObject.GetType().I...
I'm trying to get the name of a method on a type using a lambda expression. I'm using Windows Identity Foundation and need to define access policies with the type name with namespace as a resource and the method name as the action. Here is an example.
This is the type I would be getting the type name and method name from:
namespace My....
Is there any way for a SecurityManager in Java to selectively grant ReflectPermission("suppressAccessChecks") depending on the details of what setAccessible() is being called on? I don't see any way for this to be done.
For some sandboxed code, it would be very useful (such as for running various dynamic JVM languages) to allow the setA...
class Foo(){
private String x,y;
//getters setters
}
main(){
Foo bar1 = new Foo();
Foo bar2 = new Foo();
bar1.setX("hey");
bar2.setX("hi");
bar2.setY(" there");
setNewValuesFromLeftToRight(bar1,bar2);//left:bar1
System.out.print(bar2.getX+bar2.getY)// hey there
}
setNewValuesFromLeftToRight : this method wou...
I've got an IList<Delegate> that contains some Func<bool>s and some Predicate<T>s, where T varies. I later need to sort out which of these items are Predicate<T>s, but don't want to close the door to adding other Delegate types to the list later, so I do not want to do this by identifying objects by !(current_delegate is Func<bool>).
Th...
I have tried to find information about this but have come up empty handed:
I gather it is possible to create a class dynamically in Java using reflection or proxies but I can't find out how. I'm implementing a simple database framework where I create the SQL queries using reflection. The method gets the object with the database fields a...
Hello guys,
I have an Assembly Library1.dll which contains some Interfaces, which were serialized as a byte array into the database. For some reasons we have to change the Interface properties and defintion. so now i am writing a migration utility. So i have 2 versions of Library1.dll , In my utility i have created a folder where i stor...
Here's the scenario using Assembly.ReflectionOnlyLoadFrom:
Both my assembly Inspected and my reflection Application Inspector reference Assembly Dependency.
If Inspector references Dependency 1.0.0.0 and Inspected references Dependency 1.1.0.0, Inspector cannot reflect over any types or methods in Inspected that use a type from Depende...
I have a class tree, which contains multiple objects in a hierarchy. So I might have a Container object, which hosts 3 SubContainer objects, which in turn host an arbitrary number of Item objects.
Is there any way that I can use an XPath-style expression over this object tree, which would imply reflection, so that I could query a proper...
Is there any reflection performance considerations when repeatedly calling container.Resolve<T>() when a resolution has already been established?
I'm using it in an MVC controller to resolve my data service, so it will be called on every HTTP request. I'm storing the container instance in Application state, and I'm using container contr...
Let's say I have a class called Test with one property called Title with a custom attribute:
public class Test
{
[DatabaseField("title")]
public string Title { get; set; }
}
And an extension method called DbField. I am wondering if getting a custom attribute from an object instance is even possible in c#.
Test t = new Test();...
I have a static class with static private readonly member that's set via the class's static constructor. Below is a simplified example.
public static class MyClass
{
private static readonly string m_myField;
static MyClass()
{
// logic to determine and set m_myField;
}
public static string MyField
{
...
I am trying to get a string name of a class from the class object itself.
// For instance
[NSArray className]; // @"NSArray"
I have found object_getClassName(id obj) but that requires an instance be passed to it, and in my case that is needless work.
So how can I get a string from a class object, and not an instance?
...