Skip to main content

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
18 votes
4 answers
31k views

Returning Groovy class fields as a map

I want to get a map of all the fields in a class (aka value object) in a generic way. The following works fine for me: ...
neu242's user avatar
  • 291
17 votes
1 answer
60k 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 ...
Sam's user avatar
  • 335
15 votes
2 answers
16k views

Extension method to list enum values

I have the following enum extension method: ...
Pete's user avatar
  • 641
14 votes
6 answers
192k 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 ...
Sahil Sharma's user avatar
14 votes
1 answer
57k 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 ...
Vladimir Chernenko's user avatar
14 votes
2 answers
36k views

Mapping ExpandoObject to another object type

I am working on a helper method that maps properties from an ExpandoObject to a user supplied object and was wondering if the code could be cleaned up or made any ...
Tom Bell's user avatar
  • 243
14 votes
1 answer
2k views

Low Level VBA Hacking - making Private functions Public

NEW: Download demo files Not sure what to title this - essentially, inspired by RubberDuck's unit test engine, I've created a way to call private methods of standard modules in VBA. It also lets you ...
Greedo's user avatar
  • 2,595
13 votes
1 answer
7k views

Inject dependency into PostSharp aspect

I have designed couple of aspects using PostSharp for different projects, but there is a design flaw in many of them: dependency management. This is a question about injection of dependencies into ...
Akim's user avatar
  • 475
12 votes
1 answer
4k views

Mimic C#'s nameof() in Python

I rewrote some code I posted earlier this month. It attempts to mimic C#'s nameof() by extracting names from bytecode instructions. This seems more reliable than ...
user avatar
11 votes
5 answers
31k views

Convert a custom object to a query string

I wrote an extension method that takes a custom object and converts its properties to a query string for use in a URL. You can specify what properties you want included by using a ...
an earwig's user avatar
  • 445
11 votes
4 answers
6k views

Using reflection in a test to check if a private variable is really null after a function, is this okay?

I am using reflection to check if a private variable is set to null after the logout function. This is needed because the getUser function will always attempt to set and return an user if no user is ...
Wesley Egbertsen's user avatar
11 votes
2 answers
108k views

Check if any of class properties is not null/empty/was assigned

I have a class that I use to display on a web page fieldset. However, all of the properties of this class are optionally filled in by the user, which means all of them could be null/default. In this ...
victor's user avatar
  • 233
11 votes
3 answers
846 views

Give me thy name, I'll invoke thee

This generic function invokes a property of a given object by name: ...
user avatar
11 votes
3 answers
1k views

Simple generic auto __repr__

Preface At some point I was tired of writing & supporting __repr__ methods, so I've decided to write it once and reuse everywhere in my classes. Since I'm ...
Azat Ibrakov's user avatar
10 votes
3 answers
3k views

Listing human-readable enums

I'm trying to do some util class to operate on enums - convert Enums to its special strings representation via interface method. String stored in enum constructors. ...
Alex Silkovsky's user avatar
10 votes
2 answers
5k views

Override equals() and hashCode() using reflection

I wrote a utility method to override equals() using reflection. This works fine, but I wonder if this code will pass all the tests. ...
khushnood's user avatar
  • 123
10 votes
3 answers
3k views

Converting between data and presentation types

Below is how I'm solving the problem of converting between data and presentation types, I'd like to know if that's a good way to go about it, and if not, what would be a better way to go about it. I ...
Mathieu Guindon's user avatar
10 votes
3 answers
2k views

Tracking Entity Changes (not EF)

So, I kept refactoring my Sage300 API wrapper - I wanted the client code to feel just like using Entity Framework - this is the closest I got to it: ...
Mathieu Guindon's user avatar
10 votes
1 answer
1k 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 ...
Tamoghna Chowdhury's user avatar
10 votes
1 answer
269 views

Linq-to-Sage Implementation

Following-up on my last question where I wrapped the Sage API with a familiar IRepository interface, I decided to push the abstraction a step further, and... ...
Mathieu Guindon's user avatar
9 votes
5 answers
11k 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 ...
Daniel Ryan's user avatar
9 votes
2 answers
2k views

Optimize a generic foreach method that converts Datatable to my object using Reflection

I need to optimize this code block below, this method converts a Datatable object to the Object that I am passing by parameter, in another words, this serializes the object, however I have to run this ...
Roger's user avatar
  • 195
9 votes
1 answer
768 views

Change arbitrary arguments of function based on their names with a decorator

I ran across an issue that proved to be more complicated than I thought: Changing an arbitrary argument of a function purely based upon its name in Python - possibly via a decorator. I tried to ...
s-m-e's user avatar
  • 273
9 votes
1 answer
5k views

Variadic macro enum class "reflection" in C++11

I recently had the need of "reflecting" multiple enum class constructs in order to get their elements' names as std::string ...
Vittorio Romeo's user avatar
9 votes
2 answers
336 views

Immutable builder and updater

There aren't enough questions about creating immutable objects... so why not try it again with another approach. This time, it's a builder that maps properties to constructor parameters. Properties ...
t3chb0t's user avatar
  • 44.3k
9 votes
2 answers
1k views

Generic queries and handlers without reflection

I'm trying to implement generic queries and handlers so that I can make requests with simple syntax, like this: ...
Bryan's user avatar
  • 231
9 votes
1 answer
14k 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 ...
dfernan's user avatar
  • 235
8 votes
2 answers
11k views

Copy a similar list to another

My code works fine and I am just wondering is there a more efficient way to copy a similar list to another and ignore the properties which are not present. ...
Jimbo Jones's user avatar
8 votes
4 answers
9k views

Get nested type name without namespace

Suppose I have a nested class structure like this: ...
p.s.w.g's user avatar
  • 1,961
8 votes
3 answers
130 views

Modern utility to get type name strings

This is a modernization of a very old utility I’ve had in my personal code toolbox for a while. There are two variable template constants: indi::type_name<T>:...
indi's user avatar
  • 13.7k
8 votes
1 answer
628 views

Evaluate the best common type to fit both of types

Here is an algorithm to evaluate best matching System.Type for two types in hierarhy. This is one of answers for this StackOverflow question by Ken Kin: Evaluate ...
Akim's user avatar
  • 475
8 votes
2 answers
902 views

Strict types decorator (works only with Python 3.5)

I wrote a decorator which makes Python 3.5 raise exceptions if the arguments that are passed to a type-hinted function are of the wrong type. ...
Ilya Peterov's user avatar
8 votes
2 answers
2k views

Attribute driven behaviour in C# methods

We want to create a TransactionScope factory class that we can use as a central point for instantiating TransactionScopes with ...
Eoin Campbell's user avatar
8 votes
1 answer
2k views

Dynamic data object generation

Last weekend I had an idea: Why not remove dozens of classes by removing all the basic get/set/equals/hashCode implementation of my data object interfaces? I tend to code against interfaces, so I ...
Florian Schaetz's user avatar
8 votes
1 answer
621 views

Improve this reflection bashing code

I have implemented an IDataContractSurrogate to enable serialization of ImmutableList<T> (from the Microsoft Immutable ...
bradgonesurfing's user avatar
7 votes
1 answer
6k 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 ...
Der Kommissar's user avatar
7 votes
2 answers
4k views

Using Reflection to Initialize Instance Variables

I'm facing the issue of how to reconstruct data across a network in Java. As part of my Component based game engine (read about it here, I have to encode components as XML on the server, send it to ...
psychicmachinist's user avatar
7 votes
2 answers
10k views

Function that builds dictionary based on lambda params

I've written a method in c# that allows me to do create a dictionary from a passed in object and N lambda expressions that reference that objects properties and methods. It's working the way I want it ...
Ronnie Overby's user avatar
7 votes
2 answers
19k views

Setting the value of properties via reflection

I am working on an application in which I have to parse an XML file. Nodes of the XML file corresponds to an object. I have to populate the properties of the objects at run time. The code below ...
Sandeep's user avatar
  • 410
7 votes
1 answer
6k views

Getting all public constants from the main and nested classes

I wrote this recursive function to get all constants from a class and its subclasses. Can it be simplified? ...
Иван Грозный's user avatar
7 votes
2 answers
33k views

Find all collection properties of an object and get their values

I have a chunk of code that get's all of the ICollection properties of the passed in object and then takes all of their values and adds them to another ...
Gup3rSuR4c's user avatar
7 votes
2 answers
171 views

Action based controller using reflections

I'm modeling a reflection-based controller. I would like to know if you agree with my implementation and about what could be enhanced. I'm starting with reflection and I would like to know if I'm ...
user1618606's user avatar
7 votes
1 answer
3k views

JSON serializer using generics rather than System.Object

I recently created a custom static class in C# to encode, hopefully, any object it is given (or collection of objects), because I was, at the time, unaware of a native C# library that did the same ...
Joshua Kemmerer's user avatar
7 votes
2 answers
16k views

A generic way to use LINQ to Entity with types and operations unknown until run time

A question was asked here about reflection and LINQ to entity. I'm trying to modify the code that was presented to fit my needs. Here's what I am trying to accomplish: I'm writing back-end support ...
David's user avatar
  • 171
7 votes
1 answer
4k 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 ...
EJoshuaS - Stand with Ukraine's user avatar
7 votes
1 answer
204 views

My EventBus system

I decided to roll out my own EventBus system which is intended to be thread-safe. Hence a review should focus extra on thread safety apart from all regular concerns. The ...
skiwi's user avatar
  • 10.6k
7 votes
1 answer
1k 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 ...
James Schinner's user avatar
7 votes
1 answer
365 views

NullGuard for throwing more informative exceptions

This small utility class is my solution for a more convenient way for checking against null. I also wanted to have more informative NullReferenceExceptions but I ...
t3chb0t's user avatar
  • 44.3k
7 votes
2 answers
278 views

Dispatcher for a JSON API without routes

I am using this code for a fast JSON API without routes. Does this code contain any useless parts? Is the class loader efficient enough? Are there any better options? Is ...
Aalex Gabi's user avatar
7 votes
1 answer
509 views

Milking a COM type library: "fun" with COM reflection

Once upon a time, there was a duck that wanted to know where and how user code was calling into the VBA standard library and Excel object model. To match the rest of its API, the poor little duck had ...
Mathieu Guindon's user avatar

1
2 3 4 5