0

I'm trying to work out if the following is possible, I've done lots of googling and I'm a bit confused. I'm pretty sure I can work out the class reflection on the object class and updating the database isn't a problem once I have the loop, but I can't work out if this is possible at all.

If I was doing this with a fixed class object it'd be easy I'd just do:

public void updateDB(obj_Users myObject)

But as I have a lot of class objects that will take a long time, so trying to make it more dynamic.

Here is a sample of a class I want to pass through (but could be anything following the same format):

public class obj_Users
{
    public Int32 UserID { get; set; }
    public String UserName { get; set; }
    public String FirstName { get; set; }
    public String Surname { get; set; }
    public String Email { get; set; }
    public Int32 UserRole { get; set; }
    public Int32 UserCompanyID { get; set; }
    public String UserPassword { get; set; }
    public Boolean IsAdmin { get; set; }
    public String SessionKey { get; set; }
}

Here is some pseudocode of what I'm trying to achieve to explain:

public void updateDB(AnyClassObject obj_Users)
{
    // Loop through obj_Users and grab the properties

    // DB update call
}
7
  • 1
    You can get the properties using obj_Users.GetType().GetProperties(), is there anything in particular you are stuck with? Commented Dec 2, 2014 at 11:08
  • Sorry getiting the properties isnt the issue, its the being able to pass the object through to updateDB i can't work out Commented Dec 2, 2014 at 11:10
  • Is AnyClassObject an ancestor of obj_Users? Commented Dec 2, 2014 at 11:11
  • Just make the parameter of type object, then you can pass in an instance of any class or struct. Commented Dec 2, 2014 at 11:11
  • Sorry, my bad explaining. AnyClassObject doesnt exist, its psudocode for what i'm trying to achieve. I can't work out what that should be Commented Dec 2, 2014 at 11:11

4 Answers 4

0

This:

public void updateDB(object paramobj)
{
    foreach(var prop in paramobj.GetType().GetProperties())
    {
           //grab
    }
    // update
 }
Sign up to request clarification or add additional context in comments.

3 Comments

What i'm trying to achieve though is to not have to list each object in the if - i have like 100 objects so that's the bit i'm trying to make dynamic
That makes sense. How do I call that though? When i do DBCommon dbc = new DBCommon(); dbc.updateDB(obj_Users); it tells me that obj_Users is a type but used like a variable
Of course it tells you that. obj_Users is a class, you need to create an instance of it and pass it to your method.
0

Try this to loop to get the properties

foreach(var prop in obj_Users.GetType().GetProperties()) {
    //use your props
}

Comments

0

You just need your parameter to be of type object to pass an instance of any class or struct.

public void updateDB(object whatever)
{
    foreach(var prop in whatever.GetType().GetProperties())
    {
       ... //do stuff
    }
} 

Comments

0

If you're stuck with the problem of having to deal with different classes, a generic method should fit your needs:

public void UpdateDB<T>(T obj)
{
    var properties = typeof(T).GetProperties();

    foreach (var prop in properties)
    {
        // Here you loop through the properties.
    }
}

Comments

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.