public static List <Tuple <CodeTypeDeclaration, EntityMetadata> > GetEntityTypes(this CodeCompileUnit codeUnit, IServiceProvider services) { if (_entityTypes != null) { return(_entityTypes); } _entityTypes = new List <Tuple <CodeTypeDeclaration, EntityMetadata> >(); var metadata = ((IMetadataProviderService)services.GetService(typeof(IMetadataProviderService))).LoadMetadata().Entities.ToDictionary(e => e.LogicalName); foreach (var type in codeUnit.GetTypes().Where(type => type.IsClass && !type.IsContextType())) { var logicalNameAttribute = type.CustomAttributes.Cast <CodeAttributeDeclaration>() .FirstOrDefault(a => a.Name == "Microsoft.Xrm.Sdk.Client.EntityLogicalNameAttribute"); if (logicalNameAttribute == null) { continue; } var typeEntityName = ((CodePrimitiveExpression)logicalNameAttribute.Arguments[0].Value).Value.ToString(); if (metadata.TryGetValue(typeEntityName, out var entityMetadata)) { _entityTypes.Add(new Tuple <CodeTypeDeclaration, EntityMetadata>(type, entityMetadata)); } } return(_entityTypes); }
private static void AddMetadataAttributes(CodeCompileUnit codeUnit, IServiceProvider services) { if (!AddOptionSetMetadataAttribute) { return; } var metadataByName = GetMetadataForEnums(services); foreach (var type in codeUnit.GetTypes()) { if (metadataByName.TryGetValue(type.Name, out var osMetadata)) { AddMetadataAttributesForSet(type, osMetadata); } else { Trace.TraceInformation("Unable to find metadata for {0}", type.Name); } } }