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
2 votes
1 answer
367 views

Kotlin reflection to generate one class from class similar with similar properties

This code is intended to generate a class (domain) from a class with class with similar/identical properties (api). Motivation is to speed development time when using clean architecture. ...
Martin's user avatar
  • 306
3 votes
1 answer
48 views

Order loaded modules by amount of functions

I am trying to solve some exercises from the "Programming Erlang" book. One of them is "Write a function to determine which module exports the most functions" This is my solution: ...
JDurstberger's user avatar
3 votes
1 answer
418 views

CSV Reader from scratch

I am new to Java and not quite familiar with its design patterns. I have tried to implement a CSV-File reader from scratch. The CSVFile constructor accepts the path to the file and a class which ...
FooBar's user avatar
  • 133
3 votes
1 answer
4k views

Use reflection to check for class methods with a custom attribute

This code is within a ViewModelBase class. I have a custom attribute defined that is meant to trigger methods when a property is changed. The following runs as the ViewModel is being constructed. ...
Eric H's user avatar
  • 168
4 votes
2 answers
2k views

Populate null fields in an object through reflection

I have recently implemented this utility class for going through an instance's member fields recursively and assigning them default values if they are null. Here is the implementation in the ...
InstilledBee's user avatar
5 votes
1 answer
474 views

Python function to hash a python function

I'm working on a sort of distributed build system. The system allows execution of snippets of scripts as build steps. I need to be able to hash these snippets of code in a way that comments and doc ...
Luke's user avatar
  • 151
6 votes
1 answer
929 views

Fake duck typing with dynamic

There are sometimes situations where you have a couple of related classes with static fields or properties but you cannot access them via ...
t3chb0t's user avatar
  • 44.1k
2 votes
1 answer
187 views

Factory Design Pattern in Java [closed]

I am currently attempting to understand the fundamentals of factory methods in Java and I found a lot of exampels where a pattern like this has been used. I got some exercises about sorting algorithms ...
L.Spillner's user avatar
6 votes
2 answers
4k views

Custom enumeration classes

Lately I've been using a lot of enums in my project and the lack of generic Enum constraint seems to be quite problematic. This combined with few other headaches ...
Denis's user avatar
  • 8,448
3 votes
3 answers
4k views

Method to return a collection of objects, filtered by a string

I have a Vehicle object which has a few properties such as Make, Model, ...
Danny Goodall's user avatar
2 votes
1 answer
103 views

Event based input files management system

I'm currently making a system that can accept different sorts of files and some classes are interested only in specific types of files, while some don't discriminate at all. Note Type of file isn't ...
Denis's user avatar
  • 8,448
11 votes
5 answers
30k 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
6 votes
2 answers
52 views

Calculate all LED characteristics given random sufficient set of parameters

I'm shopping for LED strips and as there are so many variations I need some help by visualizing different product properties. For that I made a class that describes(holds data for) LED strips. There ...
Kristjan Kiolein's user avatar
1 vote
1 answer
117 views

Object formatter using reflection

I am making an object formatter for use when debugging. Formatted class: ...
MCMastery's user avatar
  • 203
-3 votes
1 answer
292 views

Use inheritance to reuse toString/equals/hashCode [closed]

Does it bad practice inherit class to reuse toString/equals/hashCode (that actually based on reflection and use actual class field for it)? For example: ...
Sergii Getman's user avatar
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
1 vote
1 answer
2k views

Changing object attributes by calling getters and setters using reflection

We have a class with 10 attributes (longs, Strings) the following method should change any attribute to any value of an object specified by the client, It mimics the SQL update statement ...
user2962142's user avatar
5 votes
1 answer
350 views

Frame hack to get variable names as strings

I don't claim this is useful nor that it adheres to best practices. There's also no good reason the hack is a context manager, and it won't work on the interactive shell. ...
user avatar
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
4 votes
2 answers
6k views

Test all properties with single test function

In writing unit tests for c# functions, there is a good case made for testing properties. In testing those properties, I felt there should be a few goals. Provide a wide range of inputs Test ...
StephenH's user avatar
  • 173
1 vote
0 answers
979 views

C# MVC Generic Custom Model Binder

I recently created a custom model binder as a training exercise and wanted to see how I could improve it. It currently uses recursion and works great, but I was wondering how I could convert this to ...
MattyMc's user avatar
  • 33
1 vote
2 answers
220 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 ...
Gae's user avatar
  • 31
1 vote
1 answer
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. ...
Amol's user avatar
  • 111
2 votes
3 answers
3k 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 ...
Gae's user avatar
  • 31
3 votes
0 answers
83 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, ...
bendl's user avatar
  • 131
3 votes
1 answer
261 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 ...
ThunderFrame's user avatar
  • 1,969
2 votes
1 answer
103 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 ...
jpell's user avatar
  • 123
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
5 votes
1 answer
2k 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 ...
t3chb0t's user avatar
  • 44.1k
13 votes
1 answer
53k 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
2 votes
1 answer
115 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 ...
GiantTree's user avatar
  • 628
-2 votes
1 answer
101 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 ...
RandomHandle's user avatar
9 votes
5 answers
10k 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
4 votes
2 answers
128 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 ...
SCFi's user avatar
  • 163
4 votes
2 answers
661 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 ...
rayepps's user avatar
  • 232
17 votes
1 answer
58k 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
1 vote
1 answer
114 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: ...
Eduardo Santos's user avatar
5 votes
5 answers
7k 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 ...
Lee's user avatar
  • 183
4 votes
1 answer
114 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. ...
tulians's user avatar
  • 379
5 votes
1 answer
4k 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 ...
Der Kommissar's user avatar
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
4 votes
0 answers
133 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 ...
Nolan Robidoux's user avatar
1 vote
1 answer
66 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: ...
Renan Quirino's user avatar
2 votes
3 answers
4k 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 ...
alaskanloops's user avatar
1 vote
0 answers
2k 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. ...
Dexter's user avatar
  • 415
4 votes
3 answers
4k 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 ...
Kittoes0124's user avatar
  • 1,940
1 vote
1 answer
55 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 ...
t3chb0t's user avatar
  • 44.1k
4 votes
2 answers
2k 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. ...
Manioth Shijith's user avatar
3 votes
0 answers
104 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 ...
Edwin van Mierlo's user avatar
14 votes
6 answers
186k 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