Generics and Reflection
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Generics and Reflection

  1. #1
    Join Date
    May 2011
    Posts
    18

    Talking 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?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,197

    Re: Generics and Reflection

    Not answering your question, but is there a way to approach the problem without using reflection?

  3. #3
    Join Date
    Dec 2009
    Posts
    145

    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.

  4. #4
    Join Date
    Jul 2012
    Posts
    90

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center