-
July 25th, 2012, 01:21 PM
#1
Generics and Reflection
I have a Method that accepts a generic parameter:
Code:
public void AddErrors<T>(T Validator)
{
}
I have two Validator classes (Incoming and Outgoing). Each contain an ERRORS collection. Below is an example of the OutgoingDefinition
It is defined as:
Code:
public List<Error<OutgoingErrors>> Errors {get;set;}
Error is a generic class that is a Translator of sorts that will hold a ErrorEnum and various other properties depending on the error type (i.e. Incoming/Outgoing)
What i want to do is create an instance of my Validator (either incoming or outgoing) and then pass it into my "AddErrors" method defined above.
On that validator, i want to grab the "ERRORS" property and get the value of that so that I can do something specific with it. The problem I'm having is validator.Errors doesn't compile since validator is defined as Generic.
So, I'm trying to use Reflection to grab the property info.
Code:
var errors = validator.GetType().GetProperty("Errors");
Once I grab the Errors Proprety Info, I'm trying to do a GetValue() on "errors" but I'm just haven't been able to obtain that property object
Can someone give me some pointers on how to obtain that?
-
July 25th, 2012, 06:49 PM
#2
Re: Generics and Reflection
Not answering your question, but is there a way to approach the problem without using reflection?
-
July 27th, 2012, 12:44 AM
#3
Re: Generics and Reflection
can it be implemented with its own GetType and GetProperty instead ? Or any ways to distinguish between Out and In errors. If they are all added in during failed communications, one can append some flag to make them different while logging.
-
July 30th, 2012, 12:29 PM
#4
Re: Generics and Reflection
Obtaining type information via Reflection is quite a bit more complex for Generic types than for non-generic types. See http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx for more details.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|