Ejemplo n.º 1
0
        public static Entity ToEntity <T>(T record)
        {
            FetchXmlEntityLogicalNameAttribute logicalNameAttribute = (FetchXmlEntityLogicalNameAttribute)typeof(T).GetCustomAttributes(typeof(FetchXmlEntityLogicalNameAttribute), true).FirstOrDefault();

            dynamic recordD = record;

            var properties = typeof(T).GetProperties().Where(p => p.Name != "Id");

            Entity instance = new Entity(logicalNameAttribute.LogicalName, recordD.Id);

            foreach (var property in properties)
            {
                instance.Attributes[property.Name] = property.GetValue(record);
            }

            return(instance);
        }
        private string GetLogicalName <T>()
        {
            Type   currentType = typeof(T);
            object entityLogicalNameAttribute = currentType
                                                .GetCustomAttributes(typeof(FetchXmlEntityLogicalNameAttribute), true)
                                                .FirstOrDefault();

            if (entityLogicalNameAttribute == null)
            {
                throw new InvalidOperationException($"Attribute of type {typeof(FetchXmlEntityLogicalNameAttribute)} is not applied on type {typeof(T)}");
            }

            FetchXmlEntityLogicalNameAttribute fetchXmlEntityLogicalNameAttribute = (FetchXmlEntityLogicalNameAttribute)entityLogicalNameAttribute;
            string entityLogicalName = fetchXmlEntityLogicalNameAttribute.LogicalName;

            return(entityLogicalName);
        }