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. Reflection question (for <= .NET 3.5)

Reflection question (for <= .NET 3.5)

Scheduled Pinned Locked Moved C#
helpquestioncsharpannouncement
5 Posts 3 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.
  • M Offline
    M Offline
    MarkB777
    wrote on last edited by
    #1

    Hey guys, Long story short: I have a number of classes that are structured like this:

    class ClassA
    {
    [WantToCopy]
    public Property1 { get; set; }
    public Property2 { get; set; } // ...
    }

    class ClassB : ClassA
    {
    public Property1 { get; set; }
    public Property2 { get; set; } // ...
    }

    class ClassC
    {
    public ClassB { get; set; }
    public Property1 { get; set; }
    public Property2 { get; set; } // ...
    }

    I am trying copy the ClassA values (marked with a specific attribute) from an instance of ClassB to a new instance of ClassA. This is what I am doing:

    T resp = GetClassC(); // get a pretend instance of ClassC

    var types = resp.GetType().GetProperties();
    foreach (var type in types)
    {
    // I use this to figure out if the field in ClassC == ClassB
    if(type.PropertyType.IsSubclassOf(this.GetType()))
    {
    // I use this custom method to get the properties in ClassA that have the
    // attribute mentioned above
    List fields = GetFields(type.PropertyType);
    foreach (var field in fields)
    {
    // here is the problem!
    var value = field.GetValue(resp , null); // resp is ClassC
    field.SetValue(this, value , null); // 'this' is ClassA
    }
    }
    }

    The problem is I get a TargetException when I try and get a ClassA value from a ClassC instance (which makes sense). ClassC is a generic type. I don't know anything about the ClassB member. Does anyone have any tricks that could help me out here? Or will I have to use an interface? P.S I am keen to keep this code to <= .NET 3.5 if possible. Thank you, Mark

    Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

    R P 2 Replies Last reply
    0
    • M MarkB777

      Hey guys, Long story short: I have a number of classes that are structured like this:

      class ClassA
      {
      [WantToCopy]
      public Property1 { get; set; }
      public Property2 { get; set; } // ...
      }

      class ClassB : ClassA
      {
      public Property1 { get; set; }
      public Property2 { get; set; } // ...
      }

      class ClassC
      {
      public ClassB { get; set; }
      public Property1 { get; set; }
      public Property2 { get; set; } // ...
      }

      I am trying copy the ClassA values (marked with a specific attribute) from an instance of ClassB to a new instance of ClassA. This is what I am doing:

      T resp = GetClassC(); // get a pretend instance of ClassC

      var types = resp.GetType().GetProperties();
      foreach (var type in types)
      {
      // I use this to figure out if the field in ClassC == ClassB
      if(type.PropertyType.IsSubclassOf(this.GetType()))
      {
      // I use this custom method to get the properties in ClassA that have the
      // attribute mentioned above
      List fields = GetFields(type.PropertyType);
      foreach (var field in fields)
      {
      // here is the problem!
      var value = field.GetValue(resp , null); // resp is ClassC
      field.SetValue(this, value , null); // 'this' is ClassA
      }
      }
      }

      The problem is I get a TargetException when I try and get a ClassA value from a ClassC instance (which makes sense). ClassC is a generic type. I don't know anything about the ClassB member. Does anyone have any tricks that could help me out here? Or will I have to use an interface? P.S I am keen to keep this code to <= .NET 3.5 if possible. Thank you, Mark

      Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      You're trying to read the value of a property defined in ClassA from an instance of ClassC, which doesn't inherit from ClassA. Try something like this instead:

      // This is an expensive call; cache the result:
      Type myType = this.GetType();

      // No need to examine sub-classes; if a property isn't
      // defined on this class, you won't be able to copy it:
      List propertiesToCopy = GetFields(myType);

      foreach (PropertyInfo property in resp.GetType().GetProperties())
      {
      // IsSubclassOf returns false if the types are the same:
      if (property.PropertyType.IsSubclassOf(myType) || property.PropertyType == myType)
      {
      // Get the instance of ClassB:
      var propertyValue = property.GetValue(resp, null);

          // Can't copy properties from a null instance:
          if (propertyValue == null) continue;
          
          foreach (PropertyInfo propertyToCopy in propertiesToCopy)
          {
              var value = propertyToCopy.GetValue(propertyValue, null);
              propertyToCopy.SetValue(this, value, null);
          }
      }
      

      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      M 1 Reply Last reply
      0
      • M MarkB777

        Hey guys, Long story short: I have a number of classes that are structured like this:

        class ClassA
        {
        [WantToCopy]
        public Property1 { get; set; }
        public Property2 { get; set; } // ...
        }

        class ClassB : ClassA
        {
        public Property1 { get; set; }
        public Property2 { get; set; } // ...
        }

        class ClassC
        {
        public ClassB { get; set; }
        public Property1 { get; set; }
        public Property2 { get; set; } // ...
        }

        I am trying copy the ClassA values (marked with a specific attribute) from an instance of ClassB to a new instance of ClassA. This is what I am doing:

        T resp = GetClassC(); // get a pretend instance of ClassC

        var types = resp.GetType().GetProperties();
        foreach (var type in types)
        {
        // I use this to figure out if the field in ClassC == ClassB
        if(type.PropertyType.IsSubclassOf(this.GetType()))
        {
        // I use this custom method to get the properties in ClassA that have the
        // attribute mentioned above
        List fields = GetFields(type.PropertyType);
        foreach (var field in fields)
        {
        // here is the problem!
        var value = field.GetValue(resp , null); // resp is ClassC
        field.SetValue(this, value , null); // 'this' is ClassA
        }
        }
        }

        The problem is I get a TargetException when I try and get a ClassA value from a ClassC instance (which makes sense). ClassC is a generic type. I don't know anything about the ClassB member. Does anyone have any tricks that could help me out here? Or will I have to use an interface? P.S I am keen to keep this code to <= .NET 3.5 if possible. Thank you, Mark

        Mark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        As you seem to have access to the code, why not add a copy constructor or Clone method to ClassA?

        M 1 Reply Last reply
        0
        • R Richard Deeming

          You're trying to read the value of a property defined in ClassA from an instance of ClassC, which doesn't inherit from ClassA. Try something like this instead:

          // This is an expensive call; cache the result:
          Type myType = this.GetType();

          // No need to examine sub-classes; if a property isn't
          // defined on this class, you won't be able to copy it:
          List propertiesToCopy = GetFields(myType);

          foreach (PropertyInfo property in resp.GetType().GetProperties())
          {
          // IsSubclassOf returns false if the types are the same:
          if (property.PropertyType.IsSubclassOf(myType) || property.PropertyType == myType)
          {
          // Get the instance of ClassB:
          var propertyValue = property.GetValue(resp, null);

              // Can't copy properties from a null instance:
              if (propertyValue == null) continue;
              
              foreach (PropertyInfo propertyToCopy in propertiesToCopy)
              {
                  var value = propertyToCopy.GetValue(propertyValue, null);
                  propertyToCopy.SetValue(this, value, null);
              }
          }
          

          }


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          M Offline
          M Offline
          MarkB777
          wrote on last edited by
          #4

          Thanks Richard that looks like it will do what I am trying to achieve, cheers mate!

          Mark Brock I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking

          1 Reply Last reply
          0
          • P PIEBALDconsult

            As you seem to have access to the code, why not add a copy constructor or Clone method to ClassA?

            M Offline
            M Offline
            MarkB777
            wrote on last edited by
            #5

            Thanks for your response. That is a good suggestion, but in this particular case I am trying to avoid using that approach. Much appreciated anyway

            Mark Brock I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking

            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