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

8
  • 1
    Are you sure that every type occurs just once within your class DummyReturnDto? Commented Jul 14, 2015 at 5:52
  • @TheEdge Why on earth would you want this? Commented Jul 14, 2015 at 5: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. Commented Jul 14, 2015 at 6: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 stackoverflow.com/questions/31399498/… and ultimately the link I post in my original post. Commented Jul 14, 2015 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? Commented Jul 14, 2015 at 17:15

1 Answer 1

4

This will do it:

var propertyInfo = typeof(DummyReturnDto).GetProperties()
                       .Single(p => p.PropertyType == typeof(Set1ReturnDto));
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@HimBromBeere You're right, I based my solution on are guaranteed to have classes as their types and will be unique :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.