0

Is it possible, and not to stupid, to create a methods that returns only a select few properties from a large object based on the input?

I have a large object that I can retrieve. The problem is that it takes quite awhile to fill/generate the entire object. I would like a single method that can fill parts of that object based on what I send in. Say that there are 100 properties in the object. In several cases i only need 1 of the properties of the object to do some checking, or a select few properties. I can make separate methods to get that property only, but this quickly generates alot of very similar methods and it will be a pain to work with and maintain.

The properties in the object are a mix of simple and complex. Some of them are just boolean and strings while others make call to a DB to resolve custom objects from a Id on the object. For example retrieves a customer object based on the customer Id.

I want to do something like this:

public MyObject FillSelectProperties(guid Id, params string[] properties)
{
   foreach(property in properties
     FillProperty(property);
}

Or something like that. Maybe use reflection? Or some other way that is more efficient.

Example:

var mo = FillSelectProperties(id, "Prop4", "Prop17", "Prop18");
if(mo.Prop4 == true)
   //do something with mo.Prop17 and moProp18

I dont know if this is only wishful thinking that this is solvable without generating alot of specific methods.

merger
  • 698
  • 1
  • 10
  • 28
  • 6
    I would suggest that if you need to do this, your object is far too complex and needs breaking up into smaller components. – DavidG Jan 22 '18 at 11:04
  • Why don't you use `linq` with `select`? – Bharadwaj Jan 22 '18 at 11:05
  • @KaRes Thanks for taking the time to [propose an edit](https://stackoverflow.com/review/suggested-edits/18588219), but adding random tags is not helpful. – DavidG Jan 22 '18 at 11:07
  • But you are not returning a MyObject. – paparazzo Jan 22 '18 at 11:16
  • This may help: https://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection-in-c-sharp – mshsayem Jan 22 '18 at 11:18
  • @DavidG You are not wrong, the object is complex. But the base object is from several layers down, so i cant change that, not without alot of reworking in alot of places. This is why i want to fill only parts of the object...the parts i need. – merger Jan 23 '18 at 06:56

0 Answers0