Lets imagine that class A has a lot of properties, and some of those properties are themselves classes and also have lots of properties. I have a string which tells what properties of class A I should check, and it might be a nested property like A.B.C, where B is property of A, and C is property of B. So if string is "B.C" then I need to check if C is a DataObjects.NET.ObjectModel.ReferenceField . I tried doing the following:

DataObjects.NET.ObjectModel.Field field = null;
                foreach (string FieldName in "B.C".Split('.')) {
                    if (field == null)
                        field = master.Type.Fields[FieldName];//master is A
                    else
                        field = field.Type.Fields[FieldName];
                    if (field == null)
                        return null;//failed to get field info - probably field name is incorrect
                }
                bool IsAReferenceField = (field is DataObjects.NET.ObjectModel.ReferenceField);

But the problem with this code is that field.Type always describes type A, even after first iteration. So how do I check if nested property is a ReferenceField?

asked Oct 17 '11 at 10:40

Bogdan0x400's gravatar image

Bogdan0x400
13558


One Answer:

Hello, Bogdan

Type property of the Field class can't be used to move forward by property chain. This property returns a type where field is declared, so this type is A for B property in your case.

But ReferenceField class has appropriate member. It is ReferedObjectModelType property. Using this property code would look like the following:

DataObjects.NET.ObjectModel.Type type = master;
foreach (string fieldName in "B.C".Split('.')) {
  DataObjects.NET.ObjectModel.ReferenceField refField = type[fieldName] as DataObjects.NET.ObjectModel.ReferenceField;
  if (refField==null) {
    type = null;
    break;
  }
  type = refField.ReferedObjectModelType;
}
bool IsAReferenceField = type!=null;

answered Oct 18 '11 at 03:53

Alex%20Ustinov's gravatar image

Alex Ustinov
2484

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA