1

I have a class as follows:

  public class DummyReturnDto
  {
      public Set1ReturnDto Foo { get; set; }
      public Set2ReturnDto Bar { get; set; }

      public DummyReturnDto()
      {
          Set1 = new Set1ReturnDto();
          Set2 = new Set2ReturnDto();
      }
  }

where all the properties are guaranteed to have classes as their types and will be unique. I would like to use reflection to set the value for the property given a particular type. So for Set1ReturnDto:

var propertyInfo = obj.GetType().GetProperty(Set1ReturnDto, ??????);
propertyInfo.SetValue(obj, value, null);

and then for Set2ReturnDto

var propertyInfo = obj.GetType().GetProperty(Set2ReturnDto, ??????);
propertyInfo.SetValue(obj, value, null);

EDIT:

This is part of the needed knowledge to implement requirements for Generic approach to dealing with multiple result sets from EF stored procedure

Community
  • 1
  • 1
TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • 1
    Are you sure that every type occurs just once within your class `DummyReturnDto`? – MakePeaceGreatAgain Jul 14 '15 at 05:52
  • @TheEdge Why on earth would you want this? – atlaste Jul 14 '15 at 05:57
  • Why use reflection for this? Can't you just keep an internal dictionary from type to instance and make the getters and setters delegate to that dictionary? What are you trying to achieve? Reflection is very rarely the solution. – Vincent van der Weele Jul 14 '15 at 06:46
  • @Vincent Ultimately I will be passing in a List of classes to my method using this. The ordering of the classes (not object instances) will be deliberate as I need to extract multiple result sets out of what is returned from the database and map them onto each dataset. See http://stackoverflow.com/questions/31399498/generic-approach-to-dealing-with-multiple-result-sets-from-ef-stored-procedure and ultimately the link I post in my original post. – TheEdge Jul 14 '15 at 10:11
  • To me it seems easier then to create an interface with a single method `SetProperty(Type t, object o)` and use that in your helper class rather than the generic type `T` (which removes the need for reflection). `DummyReturnDto` should implement that interface by having a dictionary from `Type` to `object`. All the getters do something like `Set1ReturnDto Foo { get { return (Set1ReturnDto)_dict[typeof(Set1ReturnDto)]; } }`. That would solve this problem, right? – Vincent van der Weele Jul 14 '15 at 17:15
  • @VincentvanderWeele The reason for the reflection will hopefully be clearer when you have a look at what I am actually trying to achieve http://stackoverflow.com/questions/31399498/generic-approach-to-dealing-with-multiple-result-sets-from-ef-stored-procedure. Ultimately the Process() method in class MultiResultsetsHelper needs to be given all the information it needs by **various** callers who will each have their own return types and each of these can have any number of parameters of varying types. – TheEdge Jul 15 '15 at 06:22
  • For me, the only valid reason to use reflection is when going from untyped to typed data. This is what your DBReader does, so yes, there reflection is essential. Your code comes in in a strongly typed environment (though the types might only be known at runtime), so reflection is not a must. You can use it though and it's gonna work, so just use whatever you think is appropriate :) – Vincent van der Weele Jul 15 '15 at 06:51
  • @VincentvanderWeele I am interested if you can see another approach that does not use reflection? In essence if you can simplify my code such that my helper method works for different result types with different properties then I would appreciate seeing any ideas you have. – TheEdge Jul 15 '15 at 10:48

1 Answers1

4

This will do it:

var propertyInfo = typeof(DummyReturnDto).GetProperties()
                       .Single(p => p.PropertyType == typeof(Set1ReturnDto));
Rob
  • 26,989
  • 16
  • 82
  • 98
  • 1
    This assumes that every type occurs just once within the class. If this is not the desired behaviour (which isn´t clear from the tgread), you´d may change the `.Single`-part accordingly. – MakePeaceGreatAgain Jul 14 '15 at 06:05
  • @HimBromBeere You're right, I based my solution on `are guaranteed to have classes as their types and will be unique` :) – Rob Jul 14 '15 at 06:06