-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeCustomizationService.cs
More file actions
86 lines (79 loc) · 3.55 KB
/
CodeCustomizationService.cs
File metadata and controls
86 lines (79 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// =====================================================================
//
// This file is part of the Microsoft Dynamics CRM SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or online documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// =====================================================================
//<snippetCodeCustomizationService>
// Define REMOVE_PROXY_TYPE_ASSEMBLY_ATTRIBUTE if you plan on compiling the output from
// this CrmSvcUtil extension with the output from the unextended CrmSvcUtil in the same
// assembly (this assembly attribute can only be defined once in the assembly).
#define REMOVE_PROXY_TYPE_ASSEMBLY_ATTRIBUTE
using System;
using System.CodeDom;
using System.Diagnostics;
using Microsoft.Crm.Services.Utility;
namespace Microsoft.Crm.Sdk.Samples
{
/// <summary>
/// Create an implementation of ICustomizeCodeDomService if you want to manipulate the
/// code dom after ICodeGenerationService has run.
/// </summary>
public sealed class CodeCustomizationService : ICustomizeCodeDomService
{
/// <summary>
/// Remove the unnecessary classes that we generated for entities.
/// </summary>
public void CustomizeCodeDom(CodeCompileUnit codeUnit, IServiceProvider services)
{
Trace.TraceInformation("Entering ICustomizeCodeDomService.CustomizeCodeDom");
Trace.TraceInformation(
"Number of Namespaces generated: {0}", codeUnit.Namespaces.Count);
#if REMOVE_PROXY_TYPE_ASSEMBLY_ATTRIBUTE
foreach (CodeAttributeDeclaration attribute in codeUnit.AssemblyCustomAttributes)
{
Trace.TraceInformation("Attribute BaseType is {0}",
attribute.AttributeType.BaseType);
if (attribute.AttributeType.BaseType ==
"Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute")
{
codeUnit.AssemblyCustomAttributes.Remove(attribute);
break;
}
}
#endif
// Iterate over all of the namespaces that were generated.
for (var i = 0; i < codeUnit.Namespaces.Count; ++i)
{
var types = codeUnit.Namespaces[i].Types;
Trace.TraceInformation("Number of types in Namespace {0}: {1}",
codeUnit.Namespaces[i].Name, types.Count);
// Iterate over all of the types that were created in the namespace.
for (var j = 0; j < types.Count; )
{
// Remove the type if it is not an enum (all OptionSets are enums) or has no members.
if (!types[j].IsEnum || types[j].Members.Count == 0)
{
types.RemoveAt(j);
}
else
{
j += 1;
}
}
}
Trace.TraceInformation("Exiting ICustomizeCodeDomService.CustomizeCodeDom");
}
}
}
//</snippetCodeCustomizationService>