reflection

Best way to create an instance of run-time determined type [duplicate]

元气小坏坏 提交于 2020-01-22 10:48:28
问题 This question already has answers here : How to create a new object instance from a Type (12 answers) Closed 6 years ago . What's the best way (in .NET 4) to create an instance of a type determined at runtime. I have an instance method which although acting on a BaseClass object may be called by instances of its derived classes. I require to create another instance of the same type as this within the method. Overloading the Method for each derived class is not practical as it is fairly

Java: runtime method resolution

别来无恙 提交于 2020-01-22 10:43:08
问题 I'm working on some dynamic invocation of code via an interpreter, and I'm getting into the sticky ugly areas of method resolution as discussed in JLS section 15.12. The "easy" way to choose a method is when you know the exact types of all the arguments, at which point you can use Class.getDeclaredMethod(String name, Class[] parameterTypes). Maybe you have to check method accessibility and the class's superclasses/superinterfaces. But this doesn't cover any of the following cases, so it's

Reflectively getting list of Spring MVC controllers matching specific url

自古美人都是妖i 提交于 2020-01-22 05:58:40
问题 How to reflectively get list of all controllers (best if not only annotated, but also specified in xml), matching some specfic url in your Spring MVC application? In case of annotated-only, @Autowired private ListableBeanFactory listableBeanFactory; ... whatever() { Map<String,Object> beans = listableBeanFactory.getBeansWithAnnotation(RequestMapping.class); // iterate beans and compare RequestMapping.value() annotation parameters // to produce list of matching controllers } could be used, but

using system.reflection to list a class fileds

烈酒焚心 提交于 2020-01-22 03:33:47
问题 i need to get a list to store all fields - values in a class the class is just a few of public const string variables as i pasted below. public class HTDB_Cols { public class TblCustomers { public const string CustID = "custID", Name = "name", CustType = "custType", AddDate = "addDate", Address = "address", City = "city", Phone = "phone", Cell = "cell"; } } this is a method that returns a list of strings that enables me to have a list of strings representing all my tables columns names ,

How to add property-level Attribute to map ColumnAttribute at runtime?

时光毁灭记忆、已成空白 提交于 2020-01-22 02:40:35
问题 From this question we have implemented the ColumnAttribute association with the corresponding property, but when Linq to SQL tries to map this property to a Column it doesn't work. Our property and mapping code(from the other question): public System.Xml.Linq.XElement Name { get { return this.name; } set { this.OnNameChanging(value); this.SendPropertyChanging(); this.name = value; this.SendPropertyChanged("Name"); this.OnNameChanged(); } } System.Data.Linq.Mapping.ColumnAttribute

Get all Fields of class hierarchy [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-21 15:12:07
问题 This question already has answers here : Retrieving the inherited attribute names/values using Java Reflection (13 answers) Closed 8 months ago . I have classes: ClassA{ public String filedA; } ClassB extends ClassA{ public String filedB; } ClassC extends ClassB{ public String filedC; } Then I create object: ClassC c=new ClassC(); c.fieldC="TestC"; c.fieldA="TestA"; c.fieldB="TestB"; After I try get all fields, I call Field[] fields=c.getClass().getDeclaredFields(); But I get array with only

why I can`t use method get(java.lang.reflect.Field#get) before changing field`s modifiers

让人想犯罪 __ 提交于 2020-01-21 12:07:09
问题 java code as follow. import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Test { public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { C c = new C(); Field field = c.getClass().getDeclaredField("NAME"); field.setAccessible(true); System.out.println(field.get(c));//Cause program exception on line 15 while using method get(java.lang.reflect.Field#get). Field modifiers = field.getClass().getDeclaredField("modifiers"); modifiers

How would I get the column names from a Model LINQ?

删除回忆录丶 提交于 2020-01-21 03:44:25
问题 I am looking to get a list of the column names returned from a Model. Anyone know how this would be done, any help would be greatly appreciated. Example Code: var project = db.Projects.Single(p => p.ProjectID.Equals(Id)); This code would return the Projects object, how would I get a list of all the column names in this Model. Thanks 回答1: I am sorry, I don't have working experience with LINQ. This is purely based on looking at MSDN. DataContext has a Mapping property, which returns an instance

Default parameters and reflection: if ParameterInfo.IsOptional then is DefaultValue always reliable?

血红的双手。 提交于 2020-01-21 03:21:05
问题 I'm looking at how ParameterInfo.IsOptional is defined (I'm adding default parameter support to an internal IOC framework), and it seems to me that, when true, there is no guarantee that ParameterInfo.DefaultValue (or indeed ParameterInfo.RawDefaultValue ) are actually the default values that are to be applied. If you look at the MSDN example given for IsOptional, it seems possible in IL to define a parameter that is optional but for which no default is supplied (given that the

Reflection: Constant variables within a class loaded via reflection

时光怂恿深爱的人放手 提交于 2020-01-20 06:40:52
问题 I have a class which has a bunch of Constant Strings. I need to load this class via reflection and retrieve those constants. I can get up to: controllerClass = Class.forName(constantsClassName); Object someclass = controllerClass.newInstance(); but I am confused on how to retrieve the fields in this class. 回答1: A quick sample on accessing fields -- Field[] fields = controllerClass.getDeclaredFields(); for ( Field field : fields ) { field.setAccessible(true); System.out.println(field.get