Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Casting objects returned by reflection to a generic object

Casting objects returned by reflection to a generic object

Scheduled Pinned Locked Moved C#
questioncsharpjsonhelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Omer S
    wrote on last edited by
    #1

    The story behind this question is simple: I'm trying to build a serializer that serializes a generic class into a custom format. public class NGConfigUnit { protected T _value; ......... ....... public T Value { get { return _value; } set { _value = value; _isDirty = true; } } } Problem is that when I access the class via reflection the values are returned as object. Even though I can check the value of T using reflection and generate an instance using a factory, I cannot reference a generic instance dynamically i.e. I cannot write something like (no support for this C#): NGConfigUnit<typeof(myval> unit = ..... This blocks me from accessing fields and properties explicitly, which is crucial for the rest of my code. Any idea how to reach am alternative? Thanks, Omer

    L 1 Reply Last reply
    0
    • O Omer S

      The story behind this question is simple: I'm trying to build a serializer that serializes a generic class into a custom format. public class NGConfigUnit { protected T _value; ......... ....... public T Value { get { return _value; } set { _value = value; _isDirty = true; } } } Problem is that when I access the class via reflection the values are returned as object. Even though I can check the value of T using reflection and generate an instance using a factory, I cannot reference a generic instance dynamically i.e. I cannot write something like (no support for this C#): NGConfigUnit<typeof(myval> unit = ..... This blocks me from accessing fields and properties explicitly, which is crucial for the rest of my code. Any idea how to reach am alternative? Thanks, Omer

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      You are probably getting a version mismatch. Try running the scenario WITHOUT rebuilding you project.

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 beta 1 - out now!
      ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

      O 1 Reply Last reply
      0
      • L leppie

        You are probably getting a version mismatch. Try running the scenario WITHOUT rebuilding you project.

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 beta 1 - out now!
        ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

        O Offline
        O Offline
        Omer S
        wrote on last edited by
        #3

        I'm afraid my question was not understood. The generic parameter type to be returned by reflection is known only at runtime, so I cannot write in my code something like: NGConfigUnit unit = fieldInfo.GetValue(obj, null) ..... - I simply don't know if the value returned shall be NGConfigUnit or NGConfigUnit or whatever ... So what I need is a way to invoke NGConfigUnit methods not using reflection and w/o knowing what T is in advance. I doubt if there's such a way, but suggestions are welcomed. Thanks, Omer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups