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 ...
t3chb0t's user avatar
  • 44.3k
23 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/...
Adriano Repetti's user avatar
17 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 ...
Ludisposed's user avatar
  • 11.5k
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 ...
Ben Aaronson's user avatar
  • 5,734
12 votes

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

You can use inspect.signature to simplify your code. ...
Peilonrayz's user avatar
  • 43.3k
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. ...
Xiaoy312's user avatar
  • 3,136
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? ...
Snowbody's user avatar
  • 8,442
10 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" ...
BCdotWEB's user avatar
  • 11.2k
9 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 ...
Dan Oberlam's user avatar
  • 7,804
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 ...
rsilva4's user avatar
  • 181
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 ...
Jeroen Vannevel's user avatar
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"...
Konrad Morawski's user avatar
7 votes

Generic Null/Empty check for each property of a class

There are several problems with the answers here, most notably that the original question doesn't seem to be answered, namely, that we want to create a clone a class with ONLY the fields that are set ...
Stacy Dudovitz's user avatar
7 votes
Accepted

Immutable builder and updater

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

Accessing and caching attributes

Let’s define an extension method on Type: ...
Dmitry Nogin's user avatar
  • 6,131
6 votes
Accepted

Python proper abstract class and subclassing with attributes and methods

I don't see much of problems with using properties as abstract attributes e.g: ...
Alex's user avatar
  • 1,622
6 votes

Fetch and print all properties of an object graph as string

You should have a guard clause against someone passing in null PropertyInfo has a PropertyType and you should use that instead of ...
CharlesNRice's user avatar
  • 4,343
6 votes
Accepted

Fetch and print all properties of an object graph as string

Bug Your code does not handle value == null everywhere. ...
dfhwze's user avatar
  • 13.9k
6 votes
Accepted

Reducing using/foreach/using nesting with a helper extension

I'd argue that no, this does not make the code easier to understand. Instead of a combination of familiar general-purpose constructs (using and ...
Pieter Witvoet's user avatar
5 votes
Accepted

Code that uses reflection to validate arguments

Repetition & Performance You can save some time and improve the performance by not getting the stack for each parameter separately. As a matter of fact you can jump right to the right frame. The <...
t3chb0t's user avatar
  • 44.3k
5 votes

Generic Null/Empty check for each property of a class

This is an interesting approach but I would prefer to do this via attributes, for example lets take the following sample class: ...
Ron Beyer's user avatar
  • 1,051
5 votes
Accepted

Building a model-view-controller application in PHP

I would consider moving your routing definition logic out of .htaccess and into your PHP code. I think you will find it much easier over the long haul to maintain your logic solely in PHP vs. being ...
Mike Brant's user avatar
  • 9,608
5 votes
Accepted

Safely order a list of objects by DateTime

You call this method SafelyOrderListByDateTimeDescending but there is nothing safe about it. I'd be really surprised if I found that a sorting function actually ...
t3chb0t's user avatar
  • 44.3k
5 votes
Accepted

Instantiating a C++ class based on an enum value

You are suffering from repetition. Repeatedly, and it's mindmeltingly repetitive. What you have is a list of types, which you repeat all over, and the corresponding enum-values. For the sake of ...
Deduplicator's user avatar
  • 19.1k
5 votes
Accepted

Test all properties with single test function

too vague & hiding test cases ...
t3chb0t's user avatar
  • 44.3k
5 votes
Accepted

Fake duck typing with dynamic

Type safety Do you think this is a good idea or do you see anything that could be improved? Language preference is obviously subjective. Many languages can achieve the same goal, albeit in a ...
Flater's user avatar
  • 5,542
5 votes

Calculating time windows for entities

Alternative approach Instead of using attributes and reflection, I'd go for a set of generic methods that take type-specific date and value-selector methods as parameters. First, a method that ...
Pieter Witvoet's user avatar
5 votes
Accepted

Enumerate all members and types with specific attributes

public static IEnumerable<AttributeCollection<T>> EnumerateCustomAttributes<T>(this MemberInfo member) where T : Attribute I'm surprised ...
Peter Taylor's user avatar
  • 24.1k
5 votes

Feedback on automatic form filling with reflection

Just to tell you upfront, your code looks good, is easy to read and everything is well named. But it can be improved nevertheless. First thing, public methods ...
Heslacher's user avatar
  • 50.4k

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