Questions tagged [reflection]

12269 questions
1

votes
1

answer
239

Views

Get all properties on a class that are a specific open generic type

If I have the following generic class: public class Gen { public T Value { get; set; } } And a class that uses it: public class Thing { public Gen GenIntProperty { get; set; } public Gen GenStringProperty { get; set; } public string NoGenericProp { get; set; } } Using reflection, how can I get just...
Bradley Uffner
1

votes
2

answer
51

Views

How to omit some parameters of structure Gin gonic

I have big structure with more than 50 params type Application struct { Id int64 `json:'id'` FullName string `json:'fullName,omitempty'` ActualAddress string `json:'actualAddress,omitempty'` ..... } I use gin-gonic and when I return application I need to omit so...
mondayguy
1

votes
2

answer
84

Views

How to use Generics or Reflection to show/hide fragments? Kotlin Android

I have a fragment manager to show/hide fragments in my app. Right now when I want to show a fragment from any activity or fragment I call this method from the manager fun show(manager: FragmentManager, fragmentTag: String, container: Int, data: Any?): Companion { var fragment = manager.findFragment...
Pablo R.
1

votes
2

answer
35

Views

How do I ignore all hidden properties of class being inherited using reflection?

Im having an issue where I need to get all properties from an object, then sort through the properties and send the values of certain properties to another service. Here is an example of the code: public class Class1 { public string A { get; set; } public bool B { get; set; } } public class Class2...
blueberrywaffle
1

votes
1

answer
28

Views

FieldAccessException when accessing protected field via reflection

Why am I getting a FieldAccessException when trying to access a protected field with reflection in the following code? using System; using System.Reflection; public class Program { public static void Main() { Foo foo = new Foo(); BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.NonPubli...
Extremelyd1
1

votes
0

answer
6

Views

Can IntelliJ be configured to hit a breakpoint in a Java class that is loaded at runtime through reflection?

I have a Java class that is being loaded at runtime through reflection for a Kafka Streams application, configured with passing the name of the class as a String, like this: streamsConfig.put(StreamsConfig.DEFAULT_TIMESTAMP_EXTRACTOR_CLASS_CONFIG, 'com.company.data.kstreams.Processor.MediaTimestampE...
mjuarez
0

votes
0

answer
20

Views

Convert a generic type constructor to non generic type constructor

For example, I have class class TestClass { } I have some method that receives the constructor of this class. I want to invoke that constructor with some generic parameter without calling MakeGenericType and GetConstructor method again. static void Main() { var ctor = typeof (TestClass).GetConstruc...
User5854
0

votes
0

answer
26

Views

how to get local variables of a method from another class

I have two classes A and B A class contains a method which has some of the local variables. now I want to fetch their name and data type in the B class. can you please help me with this. I researched about java reflection. but I found that I can not achieve this by using reflection, as local variab...
Shweta
1

votes
1

answer
719

Views

How to find all (child) sub-interfaces of a particular interface in Java?

Given an interface: public interface A {}; with inheriting interfaces: public interface B extends A {} public interface C extends A {} How can I programmatically scan to find B and C? I.e. how to do this: Type[] types = findAllSubInterfacesOfA(); // Returns [B.class, C.class] Note: Am trying to find...
Steve Chambers
0

votes
1

answer
32

Views

How do I recursively and reflectively get a list of all possible field name paths?

I am trying to get a collection of string that give me the names of the fields of all my class members, separated by .s. For example: public class Apple { public Banana MyBanana = new Banana(); public Cranberry MyCranberry = new Cranberry(); } public class Banana { public int MyNumber = 5; public C...
Evorlor
1

votes
2

answer
114

Views

In R, how do I define a function which is equivalent to `deparse(substitute(x))`?

I want to write a function in R which grabs the name of a variable from the context of its caller's caller. I think the problem I have is best understood by asking how to compose deparse and substitute. You can see that a naive composition does not work: # a compose operator > `%c%` = function(x,y)...
Metamorphic
1

votes
1

answer
63

Views

Abstract builder constructing immutable object

I have following code implementing abstract builder (as in effective java book): interface I { I ret(); } abstract class A implements I { private String s = ''; A(Builder b) { s = b.s; } @Override public I ret() { String s = 'some new string from calc.'; //HERE I NEED TO CONSTRUCT class B //THIS IS...
Bojan Vukasovic
1

votes
2

answer
71

Views

invoke the extension method of a class without using static class but by using class iteslf using reflection

I want invoke Method2 which is an extension method of a MyClass using MyClass type. I am wondering if this is possible or not. using System; using System.Linq; using System.Reflection; namespace ConsoleApplication9 { public static class MyClassStatic { public static void Method2(this ConsoleApp2.MyC...
D J
1

votes
2

answer
36

Views

Get JSON PropertyName from C# Class, like “nameof(class.prop)” for json properties?

How would I get the JSON PropertyName of the following class & Property? Something like a 'nameof()' equivilent for JSON Properties? ie, something like var jsonName = GetJSONPropertyName(SampleClass.SampleClassID); //should return 'jsoniD' public class SampleClass { public SampleClass() { } [JsonPr...
TaterJuice
1

votes
1

answer
40

Views

Getting generic type from the DeclaringType of a nested type in C# with reflection

Say I have the following class structure: public class Outer { public class Inner { } } And some code: var testType = typeof(Outer.Inner); How can I get the constructed generic type typeof(Outer), or the value of the generic typeof(string) from the testType variable?
Shane
1

votes
2

answer
33

Views

Returned interface{} type assertion

I need function that make query to databse. I use github.com/gocraft/dbr, so i create helper function: //SelectData - selects data from database func SelectData(table, fields, where string, data interface{}) interface{} { var ptr reflect.Value ptr = reflect.New(reflect.TypeOf(data)) // create new po...
Vasilij Altunin
1

votes
4

answer
1.4k

Views

getting call hierarchy of a method using reflections

i used java reflections to get methods from a class(loaded those classes).Now i want to get the call hierarchy of those methods.How can i use call hierarchy option in eclipse IDE for that?any examples or links????
Steven
1

votes
4

answer
1.7k

Views

Any java APIs to sort list of objects based on passed object's field name?

I have a list of objects and I want to sort by passing the objects' field names and an order (ascending or descending). For example, my object is: class Person{ private String name; private String age; //getters and setters .......... } I will pass List personList, String name, and boolean isAscendi...
jgg
1

votes
1

answer
684

Views

Removing a class in PHP

Is it possible to remove a class in PHP after defining it? For example: class Test { } ('Test'); $test = new Test(); //
rid
1

votes
0

answer
120

Views

If/else and while branches in Mono.Cecil

AFAIK, Cecil does not support DefineLabel and MarkLabel. What are the alternatives to use (e.g. generating Nop opcodes) to replace labels when working with if-else and while branches? For example: public void Run(bool someParam) { int someInt = 0; while (someParam) { someInt = someInt + 1; if (someI...
MiP
1

votes
0

answer
29

Views

Adobe Air Reflection through ANEs

So, I have an ANE (we call it Leader ANE) that I am accessing with reflection from another ANE (we call it Follower ANE) through Java. The idea is to have Leader ANE be able to function completely naturally without the need for Follower ANE, but if Follower ANE exists, it would alter its flow. This...
Solidak
1

votes
1

answer
106

Views

java: ERROR: “cannot find symbol – class Constructor” when making instance from String, with parameters

I searched through all the questions here that involve 'reflection' to instantiate a parameterised object using a String of the class name, but I cannot get it to work because Java doesn't recognise Constructor as a class. Class myClass = Class.forName('MyClass'); Class[] types = {Double.TYPE, Stri...
John Doe
1

votes
0

answer
49

Views

Java generics and reflection to create and set model for SimpleJdbcCall in Spring

I am trying to use reflection and generics to create models using SimpleJdbcCall. At the moment I have to create a distinct mapper per model that I add to the project. I would like to create a mapper that uses reflection to just simply map the model, so that I only have to write one mapper (in the m...
resu
1

votes
2

answer
78

Views

How do I create a delegate for a property getter from an open generic type

I have a PropertyInfo for a property on an open generic type, obtained in the following way: public interface ITest { T Item { get; } } var propertyInfo = typeof(ITest).GetProperty('Item'); I'd like to be able to create a concrete, callable delegate, filling in the class type parameter (e.g. Func) f...
Philip C
0

votes
0

answer
5

Views

Can you use reflective injection techniques with .exe files?

I've been reading about reflective DLL injection and the various methods used to implement it. I was just wondering, could you implement reflective DLL injection using an .exe file, or does it have to be a DLL to reflectively inject it?
Steve Mucci
1

votes
1

answer
43

Views

Create an instance of a class with Graphene @Page

Create an instance of a class with Graphene @Page I have a requirement where I am finding the classes at runtime and invoking its methods at runtime. While invoking its method (using reflection java), everything gets initialize except the pages/classes injected in the searched class. Say for example...
Ankit Ojha
1

votes
1

answer
124

Views

Error upon variable assignment using GTK# and Mono (C#)

public void OnCombobox1Changed (object sender, EventArgs e) { handler.name = combobox1.ActiveText; } The code sample is a handler (auto generated by gtk# using Mono's gui developer) with my own variable assignment in it. I have three classes, a win class that contains my gui components, which the sa...
SolidDave
1

votes
0

answer
42

Views

Change a method call in a private method

I'm currently migrating an old Hibernate tool from version 3.2 to the latest release. The way it works is that I'm inheriting from the Configuration class to a modified AutofetchConfiguration class. I've run into a situation where one of the methods that I need to override has been changed into a pr...
Jssson
1

votes
1

answer
191

Views

Java: how to scan classes without knowing package names?

I'm programming an API that provides functionalities based on annotations on classes. The API would be imported, so it has no knowledge of client's packages. So the question is: how to scan classes without knowing package names? I found these methods: Package.getPackages() but it only returns a subs...
Luke
1

votes
1

answer
1.2k

Views

System.Reflection.TargetException: 'Non-static method requires a target.' only with VS 2017 not with VS 2013

I have been using the KendoDynamicLinq library for years on this project with no problems. I started to use VS 2017 Community Edition instead of VS 2013 on this same project/code and if I run the project with VS 2017 I get this exception: System.Reflection.TargetException: 'Non-static method require...
emadev
1

votes
1

answer
120

Views

Reflection: get static vars of class in Swift?

Given a simple class like: class Foo { static var can: String? { didSet { 5+4 } } static let haz: String } How do I implement a method within Foo that enumerates over can and haz; without explicitly writing them out again? - E.g.: so I can print('key: '\(key\)' has value: '\(value)'') BTW: Noticed t...
A T
1

votes
1

answer
283

Views

How to call reflection methods in freemarker template?

We can iterate over class fields, annotations with some code like this in java: Class clazz; for(Field field : clazz.getDeclaredFields()) { for(Annotation annotation : field.getDeclaredAnnotations()) { for(Method method : annotation.annotationType().getMethods()) { if(method.getName().equals('nullab...
Rasool Ghafari
1

votes
1

answer
104

Views

Reflection GetMethod with a type paramenter

The following line is not running properly, and I'm not sure why. The Error: System.Reflection.TargetParameterCountException: parameters do not match signature And the line of code in question: typeof(Resources).GetMethod('LoadAll', new Type[] { typeof(System.String), typeof(System.Type)}); Displayi...
Joseph Hales
1

votes
0

answer
74

Views

How can I look up custom attributes for a class

I am trying to use Roslyn API (Microsoft.CodeAnalysis) for loading an assembly and looking up the custom attributes. Code I have so far is: var assembly = MetadataReference.CreateFromFile(tempFileName); var compilation = CSharpCompilation.Create(null).AddReferences(assembly); var assemblySymbol = (...
VKR
1

votes
0

answer
66

Views

Identify internal APIs used through reflection

I'm working on a legacy codebase, which was originally written in JDK 1.1.6. As a result there are a number of instances where internal JDK APIs are being used, especially in the RMI & Socket programming area. I need to identify and create a list of these internal APIs used throughout my codebase. I...
CuriousCoder
1

votes
0

answer
233

Views

Convert sql server datetimeoffset to C# datetimeoffset or datetime

I have a column with datetimeoffset data type. For e.g. 2018-02-19 10:08:53.8060711 +00:00 I am trying to set this value to the C# property having the same data type. In my application, I am using reflection to set the properties into the model (can't avoid reflection) Here is the sample code - Type...
Ankur Arora
1

votes
0

answer
20

Views

Maven <Export-Package> issues in two dependent projects using Java when using Reflection API classes

I'm working as Backend developer, I'm using Java 1.6, JDBC and Angular JS. From past 5 days i'm experiencing a strange behaviour in my Java Code. Here is the problem description: I have Two Maven projects 1. A - A uses internally B classes so A is dependent on B 2. B - B having few java source files...
Patan Arshad
2

votes
2

answer
23

Views

C# Performant method caller attribute usage

I am writing a client library for an API that provides both public and authenticated endpoints. I would like to easily denote which endpoints require authentication using attributes. For instance: public async Task GetPublicData() { var request = CreateRequest( 'v1/public' ); return await _httpClien...
Haus
-1

votes
0

answer
18

Views

Caching MethodInfo doesn't improve performance?

We've recently hit some performance issues by excessive/improper use of some reflection API's in our application. I've solved one of the issues which had to do with GetCustomAttribute by caching some stuff. But I'm struggling with improving the performance of another part of our code which uses Meth...
Mentha Suaveolens
1

votes
1

answer
41

Views

Invoking methods containing Hibernate transaction on external JAR/CLASS using reflection (Java EE)

Good day! I'm having problem making hibernate work on a external jar which is loaded thru reflection on my EJB. As of now I've tried three options but none of these works and just gives me error. Here are a snippet of codes I've tried. I also added comment to which line the error occurs in each opti...
Cancerous

View additional questions