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 ...
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/...
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 ...
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 ...
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 ...
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. ...
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:
...
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 ...
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. ...
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?
...
10
votes
Change arbitrary arguments of function based on their names with a decorator
You can use inspect.signature to simplify your code.
...
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 ...
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" ...
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 ...
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 ...
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:
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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"...
7
votes
Accepted
Immutable builder and updater
You use IList<> where you should use ICollection<>. I've rarely encountered a scenario where ...
6
votes
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:
...
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:
...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
reflection × 233c# × 122
java × 56
.net × 26
generics × 25
python × 23
object-oriented × 15
python-3.x × 13
performance × 12
php × 11
meta-programming × 10
linq × 9
extension-methods × 9
serialization × 8
parsing × 7
enum × 7
validation × 6
dependency-injection × 6
expression-trees × 6
c++ × 5
beginner × 5
strings × 5
recursion × 5
inheritance × 5
static × 5