Questions tagged [reflection]

Reflection is the process by which a program can observe and modify its own structure and behavior at runtime.

Filter by
Sorted by
Tagged with
1
vote
2answers
168 views

C# - exercise - object validation - reflection - generics - follow-up

Here's a C# class as an example for object validation. For this purpose I decided to use .NET reflection and tried to use generics, probably not in the correct way, that's why I'm writing this post. I ...
1
vote
1answer
3k views

Generic Comparator

I have tried to make a generic comparator with the use of reflection, but I am not sure how efficient it is or if it is the right way to do so Also, I don't know how it will handle dates. ...
2
votes
3answers
2k views

C# - exercise - object validation - reflection - generics

Just published (GitHub) a C# example for object Validation, for this purpose I decided to use the .NET Reflection and tried to use Generics. I would really like to improve coding, can you please give ...
2
votes
0answers
65 views

Scala code that loops through Java annotations decorating Scala classes

I have code written in Scala that reads annotations decorating other Scala classes. These annotations are written in Java. The sticking point is that annotations don't allow advanced data structures, ...
3
votes
1answer
159 views

Exposing .NET's internal StorageRoot via Reflection for accessing file/stream-based Structured Storage

.NET tantalizingly exposes StorageInfo and StreamInfo as public classes, but hides the ...
2
votes
1answer
85 views

Setting value of controller controls for domain objects with Reflection

Is my use of Java Reflection an OK design? Each domain object contains an annotation to declare what controller would be initialized to edit/create the object: Each field of the domain object has ...
6
votes
1answer
479 views

Decorator to return default argument values

I'm using Python 3.6. This code, used as a decorator to a python function, will cause the function to return a dict of POSITION_OR_KEYWORD arguments. I used this ...
5
votes
1answer
415 views

Extensions for setting members via expressions and reflection

I'd like to make the usage of my configuration framework easier so I created a few extensions that after getting a value from a source automatically assign it to a property or field. They should make ...
6
votes
1answer
16k views

Python class initialize with dict

how can I express this idea in Pythonic way? The problem is that I want to create class with many attributes, but don't want to initialize all in __init__(self, arg1, arg2, ..., argN). Instead of this ...
2
votes
1answer
94 views

Improvements for ugly reflection

I have a project that uses a library to save serialized objects in items and loads them again, when needed. To do the de-/serialization, this library uses Gson. Due to how Gson works and how the ...
-2
votes
1answer
72 views

Addressing “System.NullReference” Resharper warning in function [closed]

TLDR: How can I check "DeclaringType" for null without causing a further cascade of problems in the function? I have a C# LINQ query that uses reflection to list all Areas, Controllers, and Actions ...
7
votes
4answers
4k views

Instantiating a C++ class based on an enum value

I have a bit of code duplication and trying to figure a better way to reduce it. I'm looking for any suggestions. If this was in Java I could easily use reflection. The code posted is a basic ...
4
votes
2answers
111 views

Simple object comparer

Creating a simple object compare function for public system types in a class meaning only looking at the first level of a class no breaking into an object and comparing sub properties. It performs ...
4
votes
2answers
524 views

Converting recordsets to POCO

The goal this method was to emulate the way EF or Dapper return to you concrete POCO classes instead of recordsets when querying your db. Our current code is riddled with iterations over recordsets ...
14
votes
1answer
39k views

Method that returns description attribute of enum value

I have the following method in .NET Core that returns the description attribute of an enum value. I think this code can be more elegant. I'd appreciate some ...
1
vote
1answer
103 views

Create a List of any type of object based on List of HashMap from JSON

I have this method that is working as i want: ...
5
votes
5answers
4k views

Safely order a list of objects by DateTime

I have several lists of various objects that need to be ordered by one of their properties that contains a string representation of DateTime, whose property also ...
4
votes
1answer
93 views

Sleeping and invoking a task in Python 2.X and Python 3.X

I'm writing a program that needs to be compatible for both Python 2.X and Python 3.X. My problem is that, because of the differences between these two versions, I'm duplicating code. ...
5
votes
1answer
1k views

Building a model-view-controller application in PHP

I've seen at least two recent PHP questions that would do well from a Model-View-Controller ('MVC' from here-on-out in this question) setup. Now, me, being the horrible person I am, I wanted to build ...
6
votes
1answer
5k views

Python proper abstract class and subclassing with attributes and methods

The goal of the code below is to have an abstract base class that defines simple methods and attributes for the subclasses. This is part of an application that provides the code base for others to ...
4
votes
0answers
106 views

Dynamically creating login screen on Android devices

This has been an experience. I'm well above "hello world" java is not a language I use often when programming and I don't program too much. I find myself a lot on Google.... basically just about for ...
1
vote
1answer
60 views

Executing a function's flow

My __main__.py file has a function's flow, based on methods of each class instance, which each one has its own methods and attributes, like this: ...
2
votes
3answers
3k views

Saving complex business objects with reflection and recursion

In our application we have complex objects with many nesting levels, each with various properties that are important to the business. We currently serialize and persist these objects along with their ...
1
vote
0answers
695 views

Generate random instance of given Class

I was running some rightsizing tests and had to blast random objects of my various class types. Here is the code I came up with. ...
4
votes
3answers
3k views

Accessing Properties by Name

Over the weekend I decided to start work on my own version of FastMember. I began with my take on an implementation of TypeAccessor so that I could work with the ...
1
vote
1answer
41 views

Declarative DependencyProperety registration

Most properties have simple requirements so it should be possible to register their Dependency Property fields declaratively. I made a small experiment and it seems to work just fine. The first ...
4
votes
2answers
1k views

Dynamically convert property to different data type

The code finds marked attributes and parses them according to the attribute type. Is it possible to simplify this logic? Is there generic way to do it? Please suggest some way to optimise this code. ...
3
votes
0answers
71 views

Print Python docstrings when module is executed interactively

I have written a module, which should only be used by means of import, then call the functions as required. I do not want this module to do anything when ran interactively, other than printing out ...
12
votes
6answers
99k views

Generic Null/Empty check for each property of a class

I have created a method to check for null/empty values of class properties and if any null property is found I'm stopping the checking process and returning the result as true. I've used a solution ...
4
votes
1answer
2k views

Code that uses reflection to validate arguments

I wrote an answer to this question on the Software Engineering SE site and was hoping to get it critiqued. (I'm told that it's acceptable to do so). Essentially, this code uses reflection to check to ...
4
votes
0answers
771 views

Get n-th data member of a struct

To get n-th member of a struct without non-empty bases and w/o non-public members C++ now have a pretty way: ...
5
votes
3answers
276 views

Determine the class hierarchy of a class

My only concern with the following class is the line hierarchy.trimToSize() in determineHierarchy, because it is not necessary ...
4
votes
2answers
132 views

Java CLI Program Commands

As I was creating a CLI program, I realized having a ton of switch/if-then statements was a messy way to process user input. So, I set up a way to dynamically create commands as I added modules (...
4
votes
1answer
96 views

Serializing Objects to Delimited Files Part II

This is a follow up to my previous question: Serializing objects to delimited files I've added some feature enhancements, and based on suggestions from rolfl in chat, I've fixed up a couple ...
2
votes
2answers
512 views

Python deep get

I'm implementing deep_get functionality to look inside arbitrarily nested Python 2.7 objects. Primarily for further logging. This turned out to have surprising ...
4
votes
1answer
201 views

Enumerations via reflection in AS3

I'm very much used to enumerations a la Java, where each value in an enum is a distinct object. You can call functions on these objects, and they're generally ...
1
vote
1answer
222 views

Decoupled chain of responsibility in Python

I've started to learn Python today. I'm trying to write decoupled middleware or chain of responsibility pattern. In my app I have some two classes: abstract class or interface named Processable ...
2
votes
1answer
379 views

Unit test code for a private median-of-three function, using reflection [closed]

A Junit test class is used to test the class LibraryCounter. Reflection was used to test the private methods. This made the code much more complicated. ...
5
votes
1answer
135 views

Illumination as a way of instantiating new objects

Basically, what I mean by illumination is having a method create an object where the caller supplies all the dependencies. This allows us to hide our new keyword ...
1
vote
1answer
3k views

Custom Exception.ToString() formatter

I find the default Exception.ToString() formattig very difficult to read especially if there are inner exceptions involved. I guess everyone knows how it looks: <...
3
votes
2answers
1k views

Accessing and caching attributes

I have the following manager class that handles access and caching attributes from classes. I am a bit worried about the caching mechanism. Is there a better way of handling it? ...
3
votes
1answer
674 views

Serialize and deserialize n dimensional int array

I've made two symmetric methods which serialize an int array of any number of dimensions into an OutputStream, and read such an array from an InputStream. I was wondering if there is ways this code ...
7
votes
1answer
4k views

Serializing objects to delimited files

For a new project I'm going to need to be able to serialize random types to TSV or CSV files, so I write a class which can be used to serialize any object to a TSV, CSV or any other _SV file you can ...
0
votes
1answer
90 views

Business logic parallelization engine

In my Java 8 application there are 3 different classes but each of them contains one similar method, which is dedicated to parallelization of the business logic ...
3
votes
1answer
1k views

Extended WebBrowser control

I needed the ability to do a few extra things with a WebBrowser control on a .NET Windows Forms Form, and I found it needed some updates. So, I made a few. I added ...
5
votes
3answers
279 views

Determine if Class B uses Class A ( Java Reflection)

Below is a function that determines if Class B uses Class A. Currently, it tests for: Fields Superclass Constructors Methods ...
6
votes
3answers
4k views

Generic extension method that will attempt to parse a string and return it's value or default(T)

So I got sick of several things about the way TryParse works. I implemented a generic ParseOrDefault function using reflection. It appears to work as expected, but I'm not fool enough to say my code ...
0
votes
1answer
4k views

Data model decomposition into key value pairs using reflection and attributes

I'm trying to change how our system generates PDF files. Currently I have a class for each PDF that goes through the model for the PDF and fills in the properties. I want to change that to a self ...
4
votes
1answer
43 views

Invoking API calls with parameters specified using JSON

I have an instance of a Java class that I need to call some functions on, using the API exposed by another party. For each call, I need to both check for thrown exceptions, and check the value of the ...
12
votes
1answer
806 views

Corrupting Java arithmetic through reflection

Inspired by a PPCG answer, I wrote this code, which shuffles the autoboxing caches of Number subclasses. This leads to, among other things, outputting ...