Mindscape
  • Register
  • Login
  • YOUR CART IS EMPTY
home
Software
  • Developers Toolbox
  • LightSpeed
  • Raygun
  • WPF Elements
  • Web Workbench
  • Metro Elements
  • Silverlight Elements
  • NHibernate Designer
  • Phone Elements
  • WPF Diagrams
  • Simple DB management
  • Visual Tools for SharePoint
Downloads
Store
  • Buy online
  • Purchase order
  • Volume discounts
  • Reseller
  • Software license
  • Contact sales
Blog
Support
About
  • About Us
  • Contact
  • Testimonials
rss
twitter
facebook
Working with Metadata The LightSpeed Metamodel Getting Class Information Getting and Setting Fields Through Metadata

Getting and Setting Fields Through Metadata

Getting Field Values

To obtain the value of a field on a particular entity instance, call the FieldInfo.GetValue method:

Getting the value of a field on an entity instance

var referenceField = classInfo.Fields.First(f => f.PropertyName == "OrderReference");
object reference1 = referenceField.GetValue(order1);
object reference2 = referenceField.GetValue(order2);

GetValue accepts any entity, but throws an exception if the entity is not of the type on which the FieldInfo is defined.  This can be an issue when inheritance is in play: if you are processing entities from across the inheritance hierarchy, then you will probably be using a flattened collection to ensure you process all fields, but a particular entity instance won’t have the fields declared in sibling or derived classes.  To check if the FieldInfo is defined on the entity, call IsDefined:

Using IsDefined to check that it is safe to get a field value

var classInfo = EntityInfo.FromType(typeof(SalesItem));
// Not defined on all SalesItem subclasses
var publisherField = classInfo.FlattenedFields.First(f => f.PropertyName == "Publisher");
if (publisherField.IsDefined(salesItem))
{
  object publisher = publisher.GetField(salesItem);
}

FieldInfo also provides a TryGetValue method which combines the check with the retrieval, but using IsDefined and GetValue is usually more convenient if you want to cast the value to a particular type.

From LightSpeed 5 onwards, FieldInfo includes extra information on properties:

·         Validations (which returns a list of the validations defined on entities of its type)

·         IsNullable

·         IsIdentityField

Getting Association Values

When you use FieldInfo.GetValue to retrieve an association value, this causes LightSpeed to load the association if it not already loaded.  For to-one associations, the return value is the associated entity, not the EntityHolder which implements the association.

Setting Field Values

Conversely, you can set the value of a field on an entity instance using SetValue:

Setting the value of a field on an entity instance

var referenceField = classInfo.Fields.First(f => f.PropertyName == "OrderReference");
referenceField.SetValue(order, newReference);

As with GetValue, you can use IsDefined to check that the field exists on the entity instance at hand, or TrySetValue to combine the check with the set.

When you call FieldInfo.SetValue, you are directly setting the underlying CLR field.  (Remember, the LightSpeed metamodel represents fields, not properties.)  The property setter is not called.  If you have custom logic in the property setter, for example to validate or transform input values, FieldInfo.SetValue bypasses that custom logic.

Data Products

  • LightSpeed ORM
  • NHibernate Designer
  • SimpleDB Tools
  • SharePoint Tools

DevOp Tools

  • Raygun

Visual Controls

  • WPF Elements
  • WPF Diagrams
  • Silverlight Elements
  • Phone Elements

Popular Products

  • Web Workbench

    Modern web development for Visual Studio!

  • Mindscape Megapack

    All Mindscape products for one awesome price!

Quick Links

  • Forums
  • Blog
  • Register
  • Login
  • Contact us
  • Twitter
  • Facebook
  • Google+
  • YouTube
  • Linkedin
  • Rss

© Mindscape 2025. Mindscape is a registered trademark of Mindscape Limited.

  • Terms
  • Privacy