-1

Apologies as I know the title sounds ambiguous but i couldn't word it any better

So, i will try to keep it very simple and short. Let's say i have a User object with an int property caled UserID and i'm accessing it as follows :

var value = instanceofUser.UserID;

Is there way i can create a reusable method that can access a given property of a given object ? This is what i'm trying to achieve :

void GetTheValue(object parent, property nameOfProperty)
{
  var value = parent.nameOfProperty;
}

The reason behind this is that i have a similar case, and i have to rewrite the entire code block(not this exact one, but kinnda the same) over and over again. So, what would be the approach here to make something re-usable so that i can access any given property of any given object ?

I am thinking of going with Reflection or generics however, right now, i don't have the slightest idea of what to either search or implement. Any suggestions would be helpful!

Software Dev
  • 5,368
  • 5
  • 22
  • 45
  • Reflection is the way to go if you want to access a property not known at compile time. – dymanoid Aug 09 '19 at 12:26
  • @dymanoid, any example ? – Software Dev Aug 09 '19 at 12:26
  • Tried searching? – dymanoid Aug 09 '19 at 12:27
  • But calling your function requires you to specify the class and the property anyway. If you know the parent and the property you could just use `parent.nameOfProperty`? Repeated code sounds like class inheritance issue to me. – Paul Palmpje Aug 09 '19 at 12:36
  • mm8 answer is good, but you should rather not do it, unless it's some unit test or something like that. In other case this is just shooting from a cannon to a fly. – Adam Jachocki Aug 09 '19 at 12:37
  • @AdamJachocki actually, i need to repeat the same code again and again but need to access different properties of different classes. So i guess that's my only choice. – Software Dev Aug 09 '19 at 12:43
  • @AdamJachocki, can you, however, explain why ? – Software Dev Aug 09 '19 at 12:44
  • If you need to repeat the same code for different properties, can't you make it into a method and pass the value(s) to be used? So `BigBlockOfCode(parameter) { your big block of code that uses parameter }` – Paul Palmpje Aug 09 '19 at 12:49
  • @PaulPalmpje, lets say i have 3 objects and i need to access 3 different properties of the objects to perform the same task, how would i implement your idea then ? – Software Dev Aug 09 '19 at 12:51
  • @Aousafrashid just call `BigBlockOfCode(parent1.propertyOfparent1);`then `BigBlockOfCode(parent.propertyOfParent2);`etcetera. of course the properties must be of the same type (or share the same interface). Maybe I'm missing something in your case but repeated code should go into a method (or function if you want to call it that). But maybe your case it entirely different. – Paul Palmpje Aug 09 '19 at 12:56
  • @PaulPalmpje, i need to do this : `var a = object1.property1`, `var b = object2.property2` etc.etc. So your idea won't be useful here. The reason is, at runtime, i don't know the object type, neither the property type or name. – Software Dev Aug 09 '19 at 12:59
  • 1
    @Aousafrashid if you really need that then Reflection is the way to go. But it would be some sort of last resort (we do use it to generate reports based on a text definition of the properties you want in that report). But in any case repeated code should go into a reusable method. But it's still not clear to me what that repeated code is. – Paul Palmpje Aug 09 '19 at 13:05
  • @PaulPalmpje, it's related to some db operations, in MongoDb, including the code would be unnecessary. – Software Dev Aug 09 '19 at 13:09

1 Answers1

2

Try this (error handling omitted for brevity):

object GetTheValue(object parent, string nameOfProperty)
{
    return parent.GetType().GetProperty(nameOfProperty).GetValue(parent);
}

It should give the value as an object that you then will have to cast to the expected type.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • In the comments, Adam suggested that this is not the proper way to go. Can you explain why ? – Software Dev Aug 09 '19 at 12:43
  • @Aousafrashid: I'll leave that up to Adam. I cannot explain his comments. However, using reflection to set properties is not recommended unless you have to for some reason. But was trying to answer your actual question. – mm8 Aug 09 '19 at 12:47