reflection

How to set Object's properties and its value if that Object is a property of Object that is inside of the List using Reflections

馋奶兔 提交于 2019-12-24 07:34:22
问题 I had a similar question before, but this one will need a different solution. I have object on my Model and object on my service. I need to set value of Model's object property to a value of properties coming from the service's List<TicketReportPropertyEntity> if both objects' properties are the same. This is a Model: public class MyModel{ public ObjectAEntity ObjectAData { get; set; } public ObjectBEntity ObjectBData { get; set; } } ObjectAEntity has a property called "SalesAmount" This is a

How to modify an object property given its location within the object

孤者浪人 提交于 2019-12-24 07:04:40
问题 Given an object obj , I can modify its properties by using something like obj.a.b.c = "new value" . However, I want to be able to do this programmatically, with the property's location in the form of an array. How can I make a function that looks like this: modifyProperty(obj, ["a", "b", "c"], "new value"); and is equivalent to obj.a.b.c = "new value"; ? 回答1: You could use Array#reduce for it, with a default object if no object is available. function modifyProperty(object, path, value) { var

How to convert string to code in vb.net and to view static or public variables of main project [duplicate]

久未见 提交于 2019-12-24 07:04:04
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: How to expose current assembly to CodeProvider I wrote a code which converts a string from a textbox in vb.net like in this exemple: how to convert string to code in c# But In this class I can't call any static variables and methods from main project. That is code vb.net: Dim vb As New VBCodeProvider() Dim icc As ICodeCompiler = vb.CreateCompiler() cp = New CompilerParameters cp.ReferencedAssemblies.Add(

Use cases for java annotation and more

怎甘沉沦 提交于 2019-12-24 06:49:30
问题 as far as i know, the only way to make the annotation take effect is reflection. Without reflection, java annotation become useless. Is this right? public class Main{ @SomeAnnotation(name="some",value="annotation") public void method(){ //do something. } public static void main(String...args){ Main main = new Main(); //Just call the method directly, How to make the @SomeAnnotation make sense? main.method(); } } For instance, you first get a java Method object, then check all the annotations

instantiate an object and invoking a method via a .class file using reflection in JAVA?

我的梦境 提交于 2019-12-24 06:41:05
问题 I have a .class file and know nothing about the .class file, including class name, fields or methods. What I need to do is finding out everything about the class and instantiate an object of the class. Remember that we know nothing about inside the .class file, after calling getDeclareMethod() ,getDeclareFields() and getDeclareConstructor(), I know that it contains a filed of String str and a void method with one String parameter which try to assign a name to the variable str and a pure

Using Reflection to analyze Parameters and their values

天涯浪子 提交于 2019-12-24 05:34:20
问题 I've seen older posts here on SO, about one year old which would mean that they do not really cover .NET 4 or maybe even 3.5 on this topic. So here goes. If you with reflection were to fetch parameters for the current method ParameterInfo[] methodParams = MethodInfo.GetCurrentMethod().GetParameters(); Looping through each parameter will let you fetch the parameter-name however, there is only a "DefaultValue" which I guess is there because of the new Dynamic Parameters in .NET 4. However, my

How to generically implement calling methods stored in a HashMap?

我的未来我决定 提交于 2019-12-24 05:17:09
问题 I want to route certain chars to methods, so that when the char is typed in the command-line the method is then executed. Based on the answer How to call a method stored in a HashMap, I'm mapping these chars to methods by using the "Command" design-pattern. However I want to generically implement this, so it seems that I need to implement reflection in order to use the Method class as a parameter. My attempt is getting a NullPointerException on the field private Method method in my anonymous

java reflection System.out.println

匆匆过客 提交于 2019-12-24 05:12:11
问题 I m learning java reflection. I tried System.exit(3); Class.forName("java.lang.System").getMethod("exit", Integer.TYPE).invoke(null, 3); and it works. I successfully also ran System.out.println(Class.forName("java.lang.System").getMethod("currentTimeMillis").invoke(null)); Now how can i invoke System.out.println reflectively java.lang.Class.forName("java.lang.System").getMethod("out.println", String.class).invoke(null, "Hi!"); is giving error. I know System does not have out function. So

I am using reflection to get property names and values in ASP.NET need some advice on optimization

[亡魂溺海] 提交于 2019-12-24 04:41:28
问题 I am using reflection to get property (as in {get; set} properties) names and their value. I would like to optimize this reflection. I don't have access to the code of the client classes I am using the reflection on, but after figuring out the property names of the class involved I will be reusing the same properties again and again. I am doing this in an ASP.NET application and so I was thinking of storing some cached results in the Application (HttpContext.Current.Application) so the first

Error binding to target method

扶醉桌前 提交于 2019-12-24 04:05:29
问题 MethodInfo method = typeof(T).GetMethod("Parse", new[] { typeof(string) }); parse = Delegate.CreateDelegate(typeof(Func<T,string>), method); T is a float in this case. However I am getting a Error binding to target method. Parse I believe is a static method. I have looked at other examples, but I can not figure out why it is not binding. 回答1: you have to swap T and string because the method returns a T not a string . I replaced T with float and following code works for me: MethodInfo method =