You could try something like this Tim -
AttributeReference Ent = trans.GetObject(result.ObjectId, OpenMode.ForWrite) as AttributeReference;
then, before you use the 'Ent', test if it's not null which is the return when using 'as' if it is not the correct type like so -
if(Ent != null)
{
//you have your proper object, work on it:
}
or similarly -
if(Ent == null)
return; //not what we wanted, return quietly: