reflection

Java Generics & Reflection

北慕城南 提交于 2020-01-05 10:33:41
问题 this probably is a basic question, but can I do something like this: Class myClass = Class.forName("Integer"); SomethingSimple<myClass> obj; Where SomethingSimple is a very simple generic class: class SomethingSimple<T> { T value; SomethingSimple() {} public void setT(T val) { value = val; } public T getT() { return value; } } Obviously, the code above is not correct, since myClass is an object of type Class, and a class is required. The question is how can this be achieved. I read the other

how to create an instance of class and set properties from a Bag object like session

徘徊边缘 提交于 2020-01-05 09:27:34
问题 the class will be declared in runtime and values are stored in a Bag object like session or ViewBag . Now I want to create an instance of the class and set it's properties using Bag data . I know that I should use reflection , but I don't know if there is any method out of box that does such things or I should create one ? session["Foo"] = "foo"; session["Bar"] = "bar"; var instance = System.Activator.CreateInstance(Type.GetType(className)); instance = ? // how to set properties using session

Reflection and private delegate fields of events in c#

梦想的初衷 提交于 2020-01-05 08:58:16
问题 I'm probably doing something silly, but here it goes. I'm trying to get the FieldInfo from a public event via reflection. Check this function: public void PlotAllFields(Type type) { BindingFlags all = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; FieldInfo[] fields = type.GetFields(all); Console.WriteLine(type + "-------------------------"); foreach (var fieldInfo in fields) { Console.WriteLine(fieldInfo.Name); } } public class Bar : Foo {} public class Foo { public

How to invoke a non-public method through Reflection without worrying about the method visibility?

坚强是说给别人听的谎言 提交于 2020-01-05 08:56:28
问题 I'm just testing reflection things for curiosity and I'm trying to invoke a private method but I can't find it because is Private . But what I really would like to know is if the visibility of the method could be automatically retrieved in the reflection search to don't worry about if the method to invoke is private, shared, friend, public etc... so there is a BindingFlags flags combination to be able to invoke a method indifferently of which is the method visibility?, I mean without worrying

Return instance using reflection in C#

瘦欲@ 提交于 2020-01-05 08:27:52
问题 A sample code I tried to return an instance of class is given below. public object getConstructorclass(int i) { if(i==1) { Type type = Type.GetType("test1"); }else { Type type = Type.GetType("test2"); } return Activator.CreateInstance(type); } var objcls = getConstructorclass(1); objcls.callclass();//error occured How can I mention the class type here since the type is not known at compile time but it will decided at runtime.In the above example i just pass a value 1 (it can be anything and

Building OrderBy-expression at runtime by property-name which can be nested

人盡茶涼 提交于 2020-01-05 07:36:20
问题 How can I dynamically build an order-by expression when only knowing the name of the property (or even the name of a sub-property)? What I'm trying to achieve is something like: dbResult = // some database-query as IQueryable<TSource> which is not yet executed; if (!string.IsNullOrEmpty(request.OrderBy)) { // the user want's to group the results var grouped = dbResult.GroupBy(/* this should be build dynamically */); } I need something to start with as GroupBy is awaiting a Func<TSource, TKey>

golang recurisive reflection

試著忘記壹切 提交于 2020-01-05 05:54:51
问题 I am trying to reflect recursively over a struct, printing out the type of each field. Where a field is an slice of structs, I'd like to be able to identify the type held in the array and then reflect over that type. Here is some sample code package main import ( "log" "reflect" ) type child struct { Name *string Age int } type Parent struct { Name string Surname *string Children []*child PetNames []string } func main() { typ := reflect.TypeOf(Parent{}) log.Printf("This is a : %s", typ.Kind()

Create Expression<Func<T,K>> in .Net Runtime

与世无争的帅哥 提交于 2020-01-05 05:54:46
问题 there exists a nice database called LiteDB. What I find inconvenient is an absence of attributes for specifying the relation type (value/reference) between entities, though LiteDB provides fluent interface for hardcoding it (details: https://github.com/mbdavid/LiteDB/wiki/DbRef). I am lazy guy and don't want always update this hardcoded relations to follow the changes in my data model. So I decided to realize the runtime discovery of the data model entities with the properties attributed by

Java: Using Class.forName to dynamically get a new instance of itself

谁说我不能喝 提交于 2020-01-05 04:23:10
问题 Lets say I have a class named Dog which is a subclass of Animal I have a method that needs to create an instance of a specific subclass of animal based on a string parameter public void createAnimalType(String animalType) { Class clazz = Class.forName(animalType); //Check if animalType equals Dog, or Cat, or Fox, etc // Example Dog dog = (Dog) clazz.newInstance(); ... Is there a way to crate an instance of itself (Dog, Cat, etc, Not of type Animal) without using an explicit cast to the

How to get an arrow function's body as a string?

心不动则不痛 提交于 2020-01-05 04:20:10
问题 How to get code as string between {} of arrow function ? var myFn=(arg1,..,argN)=>{ /** *Want to parse * ONLY which is between { and } * of arrow function */ }; If it is easy to parse body of simple function : myFn.toString().match(/function[^{]+\{([\s\S]*)\}$/)[1]; is enough . However, Arrow function does not contains function keyword in its definition . 回答1: This is my attempt: function getArrowFunctionBody(f) { const matches = f.toString().match(/^(?:\s*\(?(?:\s*\w*\s*,?\s*)*\)?\s*?=>\s*){