tags:

views:

1547

answers:

5

I would like to automatically generate SQL statements from a class instance. The method should look like Update(object[] Properties, object PrimaryKeyProperty). The method is part of an instance (class, base method - generic for any child). Array of properties is an array of class properties, that will be used in update statement. Property names are equal to table field names.

The problem is that I can't get property names.

Is there any option to get a property name inside class instance? sample:

public class MyClass {
public int iMyProperty { get; set; }
public string cMyProperty2 { get; set; }
{

main() {
 MyClass _main = new MyClass();

_main.iMyProperty.*PropertyName* // should return string "iMyProperty"

{

I am aware of PropertyInfo, but I don't know hot to get the ID of a property from GetProperties() array.

Any suggestion?

+2  A: 

You can do something like this:

Type t = someInstance.getType();

foreach (MemberInfo mi in t.GetMembers())
{
    if (mi.MemberType == MemberTypes.Property)
    {
        Console.WriteLine(mi.Name);
    }
}

to get all the property names for instance's type.

Vinay Sajip
He doesn't want all property names, just specific ones
George Mauer
Not sure that merits a downvote. The question says, "is there any option to get property name inside class instance?" I've shown a loop which goes through printing *all* the property names in a class instance, but he can all get the ones he wants to update into an array which he passes to the Update procedure. I don't see what the problem is.
Vinay Sajip
A: 

Since you already have an explicit handle to the specific property you want, you know the name - can you just type it?

Rex M
I think he wants the code to work for derived classes as well, so he needs to use reflection.
Zach Johnson
The main idea is not to duplicate names. So when a class (with attributes) is written I get an automatic table generation, CRUD statements and *Intellisense*.
FrenkR
+6  A: 

Just wrote an implementation of this for a presentation on lambdas for our usergroup last Tuesday.

George Mauer
A: 

You can get the name (I assume that's what you meant by ID) of a property using PropertyInfo.Name. Just loop through the PropertyInfo[] returned from typeof(className).GetProperties()

foreach (PropertyInfo info in typeof(MyClass).GetProperties())
{
    string name = info.Name;
    // use name here
}
Zach Johnson
A: 

let's say(from the first sample, method update of a class MyClass):

public class MyClass {

public int iMyStatusProperty { get; set; } public int iMyKey { get; set; }

public int UpdateStatusProperty(int iValue){ this.iMyStatusProperty = iValue; return _Update( new[iMyStatusProperty ], iMyKey); // this should generate SQL: "UPDATE MyClass set iMyStatusProperty = {iMyStatusProperty} where iMyKey = {iMyKey}" }

{iMyStatusProperty}, {iMyKey} are property values of a class instance

So, the problem is how to get property name (reflection) from a property without using names of properties as strings(to avoid field name typo-s). Rgds, Frenk

FrenkR