33 votes
Accepted

Method that returns description attribute of enum value

Type type = input.GetType(); MemberInfo[] memInfo = type.GetMember(input.ToString()); You should use more meaningful variable names like ...
user avatar
  • 43.8k
21 votes
Accepted

Generic Null/Empty check for each property of a class

First of all I'd reduce indentation. It makes your code really too hard to read. Doing that you will see it may be simplified. First of all try/...
user avatar
14 votes
Accepted

Python class initialize with dict

Python and the power of unpacking may help you in this one, As it is unclear how your Class is used, I will give an example of how to initialize the dictionary with unpacking. This will work on any ...
user avatar
  • 11.4k
13 votes
Accepted

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

Naming: Don't use variable names such as t. They're not meaningful, not for you, not for others. In this case, use type. Class ...
user avatar
  • 5,553
13 votes
Accepted

Copy a similar list to another

Small things Your code styling is generally good, but a few possible improvements: You can add a second generic type parameter to your method to avoid the need to cast to ...
user avatar
  • 5,714
10 votes
Accepted

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

A few points tickle: Why is it a static method? None of the comments are helpful. Good comments say why, not what. I'd remove them all. Bracing is inconsistent. ...
user avatar
10 votes
Accepted

Listing human-readable enums

Method header Your method header can be improved, the generic type E is not necessary, it can instead be written as: ...
user avatar
10 votes
Accepted

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

Personally I wouldn't do this. The reason why is when your field change name, your test fails because the field is hardcoded there and with refactoring this isn't persisted to the test. What should ...
user avatar
  • 3,852
10 votes
Accepted

Serializing objects to delimited files

readonly will not make members of your static serializers readonly. While you cannot reassign another serializer to replace it, its members can still be modified. ...
user avatar
  • 3,096
10 votes

Copy a similar list to another

for (int i = 0; i < propInfos.Length; i++) Any particular reason why you're not using foreach (var prop in PropInfos) here? ...
user avatar
  • 8,392
10 votes

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

You can use inspect.signature to simplify your code. ...
user avatar
  • 41.6k
9 votes
Accepted

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

Just a few things to point out: There's no need to use inline yourself. For modern compilers, it merely serves as a suggestion, but they can otherwise determine ...
user avatar
  • 34.7k
9 votes

Convert a custom object to a query string

I wouldn't go the StringBuilder route, simply because you need to handle the "what if I'm at the start/end" problem. Instead, simply collect the "URL_translated" ...
user avatar
  • 10.6k
8 votes

Returning Groovy class fields as a map

I took inspiration from @tim_yates answer and defined a Mappable trait which handles nested Mappable objects. The Trait (as ...
user avatar
  • 181
8 votes
Accepted

Override equals() and hashCode() using reflection

It ignores values in superclasses. You might want to check that too. Anyway, don't reinvent the weel, there is a library for that! I guess ...
user avatar
  • 29.7k
8 votes

Give me thy name, I'll invoke thee

LINQ has two syntaxes to offer. You've used the method syntax, which I find very neat and useful, but for simpler queries I find the query syntax looks much less bulky in VB.NET: ...
user avatar
8 votes
Accepted

Extension method to list enum values

As written in the relative MSDN page, the cause of the warning is The parameter signature of an externally visible generic method does not contain types that correspond to all the type parameters ...
user avatar
  • 2,046
8 votes
Accepted

Custom Exception.ToString() formatter

private const string Space = " "; There's little point in storing this in a variable: it's never going to change anyway and you're not clarifying any of the ...
user avatar
8 votes

Convert a custom object to a query string

In places where the type is obvious (or irrelevant) then feel free to use var instead of the type name. Also, consider not naming your variable after the type; this ...
user avatar
  • 7,673
7 votes

Override equals() and hashCode() using reflection

You should check if the references are equal first to save a whole lot of comparing when there is no need to. If both objects are null your method will return false ...
user avatar
  • 20k
7 votes

Avoid static initializer for lists of image and video formats?

According to your comment, if it's only the MultipartFileWrapper.class.getField("multipartFile") statement that might throw a ...
user avatar
  • 107k
7 votes

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

The answer from @chillworld answer your question but there is still some really smalls details in your code. I like that you're using import static for the ...
user avatar
  • 6,661
7 votes

Extension method to list enum values

Naming Type enumType = typeof(T); this name is misleading because one would assume that its always an enum which you later check by reading the ...
user avatar
  • 49.9k
7 votes
Accepted

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

For starters, I feel inclined to go meta and question the design. One possible shortcoming of your method is that it falls back to the default value, which - for primitives - isn't null. So "blah blah"...
user avatar
7 votes
Accepted

Immutable builder and updater

You use IList<> where you should use ICollection<>. I've rarely encountered a scenario where ...
user avatar
  • 12.2k
6 votes

Action based controller using reflections

I would make some changes to getFirstUpper(): ...
user avatar
  • 34.7k
6 votes
Accepted

Generic queries and handlers without reflection

One objection I could raise is that your generic type constraint doesn't guarantee what you think it might guarantee. Eg. you could have: ...
user avatar
6 votes

Java MVC model for large scale GUI using annotations

Wow. The idea is nice, but I'll say it clearly: You failed at creating a beautiful, testable and maintainable architecture. Miserably :( But let's start with first things first: ...
user avatar
  • 25k
6 votes

Type Name or Alias via Reflection for TT Code Generation

bool bAlias Don't use Hungarian notation on names. When you look at the parameter, you can already see that it's a bool, there ...
user avatar
  • 24k

Only top scored, non community-wiki answers of a minimum length are eligible