Indeed, it works better with this : 

var type = obj.GetType(); 
var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | 
BindingFlags.FlattenHierarchy); 



Thank you very much Nicholas ! 

----- Mail original -----
De: "Nicholas Kilian" <[email protected]> 
À: [email protected] 
Envoyé: Mardi 16 Novembre 2010 15:36:19 
Objet: RE: Get PrimaryKey Value without consider class type 




Er, also get the type of the instance of the object being passed to 
getPrimaryKey , rather than from the type itself. 





From: Nicholas Kilian [mailto:[email protected]] 
Sent: 16 November 2010 04:34 PM 
To: '[email protected]' 
Subject: RE: Get PrimaryKey Value without consider class type 



It’s not explicit on your classes, but I assume they inherit from 
ActiveRecordBase? 



Try using FlattenHierarchy as a binding flag. 



var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | 
BindingFlags.FlattenHierarchy); 







From: [email protected] 
[mailto:[email protected]] On Behalf Of Laurent ALBAREDE 
Sent: 16 November 2010 04:21 PM 
To: [email protected] 
Subject: Get PrimaryKey Value without consider class type 




Dear All, 





I have to access to the primary key value whatever the class used and without 
consider field/property name. Is it possible with AR to access the value of a 
PrimaryKey by the attribute name ? 


For example : 





Class A 


{ 


int _pkA; 





[PrimaryKey] 



public int PkA 


{ 


get{ return _pkA; } 


set{ _pkA = value; } 


} 


} 






Class B 


{ 


int _pkB; 





[PrimaryKey] 



public int PkB 


{ 


get{ return _pkB; } 


set{ _pkB = value; } 


} 


} 





Class Job 


{ 


public obj getPrimaryKey(ActiveRecordBase obj) 


{ 


// have to return the PK value of obj 


} 





} 





I have tried this but it doesn't work with ActiveRecordBase type: 






var type = typeof(ActiveRecordBase); // Work with typeof(A) 


var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); 


foreach (var prop in props) 


{ 


var atts = prop.GetCustomAttributes(typeof(PrimaryKeyAttribute), true); 


var propvalue = prop.GetValue(obj, null); 


} 








Sincerely, 





Laurent. 

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group. 
To post to this group, send email to [email protected] . 
To unsubscribe from this group, send email to 
[email protected] . 
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en . 

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group. 
To post to this group, send email to [email protected]. 
To unsubscribe from this group, send email to 
[email protected]. 
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en. 

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to