public static EntityReferenceWrapper ToEntityReferenceWrapper <TClass>(this Guid objectId) where TClass : class { EntityReferenceWrapper returnValue = null; if (objectId == Guid.Empty) { return(null); } MemberInfo info = typeof(TClass); var schemaAttr = info.GetCustomAttributes(typeof(CrmSchemaName), false).OfType <CrmSchemaName>().FirstOrDefault(); if (schemaAttr != null) { string entityName = schemaAttr.SchemaName; returnValue = new EntityReferenceWrapper { Id = objectId, LogicalName = entityName }; return(returnValue); } return(null); }
public static EntityReferenceWrapper ToEntityReferenceWrapper(this object entityObject) { EntityReferenceWrapper returnValue = null; if (entityObject == null) { return(null); } System.Reflection.MemberInfo info = entityObject.GetType(); var schemaAttr = info.GetCustomAttributes(typeof(CrmSchemaName), false).OfType <CrmSchemaName>().FirstOrDefault(); if (schemaAttr != null) { string entityName = schemaAttr.SchemaName; var id = entityObject.GetType().GetProperty("Id").GetValue(entityObject, null); var nameProperty = entityObject.GetType().GetProperty("Name") ?? entityObject.GetType().GetProperty("Subject"); var name = nameProperty.GetValue(entityObject, null); if (id != null && name != null) { returnValue = new EntityReferenceWrapper(); returnValue.Id = (Guid)id; returnValue.Name = name.ToString(); returnValue.LogicalName = entityName; } return(returnValue); } else { return(null); } }