protected CodeExpression GenerateSingleton(CodeTypeDeclaration @class, CodeTypeReference type, string fieldName, CodeExpression initializer) { var cctor = Declare.Constructor( ).Static( ) .AddComment(SingletonBeforeFieldInitComment); if (CodeDomProvider.Supports(GeneratorSupport.NestedTypes)) { var lazyType = Declare.NestedClass(fieldName).Private( ).Static( ) .AddTo(@class); cctor.AddTo(lazyType); Declare.Field(type, SingletonFieldName).Internal( ).Static( ) .Initialize(initializer) .AddTo(lazyType); return(Code.Type(fieldName).Local( ) .Static( ) .Field(SingletonFieldName)); } else { Declare.Field(type, fieldName).Private( ).Static( ) .Initialize(initializer) .AddTo(@class); if ([email protected] <CodeTypeConstructor> ( ).Any( )) { @class.Members.Add(cctor); } return(Code.Static( ).Field(fieldName)); } }
public static CodeMemberField AddField(this CodeTypeDeclaration type, string fieldName, CodeTypeReference fieldType, CodeExpression init = null) { var field = Declare.Field(fieldName, fieldType); if (init != null) { field.InitExpression = init; } return(type.AddMember(field)); }
private static CodeTypeDeclaration Build(string className, MemberAttributes memberAttributes, IList <ResourceMapping> map, string extensionType, bool generateConstructors, string bindingType, CodeAttributeDeclaration bindingTypeConverter) { var type = Declare.Class(className + "Extension") .Modifiers(memberAttributes); var keyEnum = Declare.NestedEnum(ResourceKeyEnumName) .Modifiers(memberAttributes) .AddSummary(ResourceKeyEnumNameSummary) .AddTo(type); var keyEnumType = Code.Type(ResourceKeyEnumName).Local( ); var objectType = Code.Type <object> ( ); type.BaseTypes.Add(Code.Type(extensionType, Code.NestedType(type.Name, ResourceKeyEnumName).Local( ))); if (generateConstructors) { var distinctNumberOfArguments = map.Select(mapping => mapping.NumberOfArguments) .DefaultIfEmpty(0) .Distinct( ) .OrderBy(numberOfArguments => numberOfArguments); foreach (var numberOfArguments in distinctNumberOfArguments) { var ctor = new CodeConstructor( ) { Attributes = memberAttributes }.AddTo(type); for (var argument = 0; argument < numberOfArguments; argument++) { var parameterName = Format(CultureInfo.InvariantCulture, FormatMethodParameterName, argument); ctor.Parameters.Add(objectType.Parameter(parameterName)); ctor.BaseConstructorArgs.Add(Code.Variable(parameterName)); } } } var keyPathType = Code.Type(bindingType); var _key = Code.This( ).Field("_key"); var _keyPath = Code.This( ).Field("_keyPath"); var _type = Code.This( ).Field("_type"); Declare.Field(keyEnumType, _key.FieldName).AddTo(type); Declare.Property(keyEnumType, "Key").Public( ).Override( ) .Get(get => get.Return(_key)) .Set((set, value) => set.Add(Code.Assign(_key, value))) .AddTo(type); Declare.Field(keyPathType, _keyPath.FieldName).AddTo(type); Declare.Property(keyPathType, "KeyPath").Public( ).Override( ) .Get(get => get.Return(_keyPath)) .Set((set, value) => set.Add(Code.Assign(_keyPath, value))) .Attributed(bindingTypeConverter) .AddTo(type); Declare.Field <Type> (_type.FieldName).AddTo(type); Declare.Property <Type> ("Type").Public( ).Override( ) .Get(get => get.Return(_type)) .Set((set, value) => set.Add(Code.Assign(_type, value))) .AddTo(type); Declare.Property(Code.Type <ILocalizer> ( ), "Localizer").Protected( ).Override( ) .Get(get => get.Return(Code.Type(className).Local( ).Static( ).Property(LocalizerPropertyName))) .AddTo(type); var translation = new CodeArrayCreateExpression(Code.Type <string> ( )); var index = 0; foreach (var mapping in map) { Declare.Field(keyEnumType, mapping.Property).Const( ) .Modifiers(memberAttributes) .Initialize(Code.Constant(index++)) .AddSummary(ResourceKeyFieldSummaryFormat, mapping.Resource.Name) .AddTo(keyEnum); translation.Initializers.Add(Code.Constant(mapping.Resource.Name)); } var translator = Declare.Field <string []> (ResourceKeyTranslatorFieldName).Static( ) .Initialize(translation) .AddTo(type); var translate = Declare.Method <string> ("KeyToName", MemberAttributes.Family | MemberAttributes.Override) .AddTo(type); var key = Code.Variable(ResourceKeyParameterName); var first = keyEnumType.Static( ).Field(map [0].Property); var last = keyEnumType.Static( ).Field(map [index - 1].Property); translate.Parameters.Add(keyEnumType.Parameter(ResourceKeyParameterName)); translate.Statements.Add(Code.If(key.IsLessThan(first).Or( key.IsGreaterThan(last))) .Then(Code.Throw <ArgumentOutOfRangeException> (Code.Constant(ResourceKeyParameterName)))); translate.Statements.Return(Code.Static( ) .Field(ResourceKeyTranslatorFieldName) .Indexer(Code.Variable(ResourceKeyParameterName).Cast <int> ( ))); return(type); }
protected virtual void GenerateClassMembers(CodeTypeDeclaration @class) { var editorBrowsable = Declare.Attribute <EditorBrowsableAttribute> (Code.Constant(EditorBrowsableState.Advanced)); var notifyCultureChanged = (CodeExpression)null; var ctor = Declare.Constructor( ) .AddSummary(ConstructorSummaryFormat, settings.ClassName) .Attributed(Declare.Attribute <SuppressMessageAttribute> ("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")) .AddTo(@class); if (settings.AccessModifiers.HasBitMask(MemberAttributes.Static)) { ctor.Private( ); } else { ctor.Modifiers(settings.AccessModifiers); } if ((settings.Options & ResourceTypeOptions.CultureChangedEvent) == ResourceTypeOptions.CultureChangedEvent) { GenerateCultureChangedEvent(@class, out notifyCultureChanged); } var resourceManager = GenerateSingleton(@class, settings.ResourceManagerType, ResourceManagerFieldName, settings.ResourceManagerInitializer); if (settings.LocalizerType != null) { var localizer = GenerateSingleton(@class, settings.LocalizerType, LocalizerFieldName, settings.LocalizerInitializer); Declare.Property(settings.LocalizerType, LocalizerPropertyName).Static( ) .Modifiers(settings.AccessModifiers) .Get(get => get.Return(localizer)) .AddSummary(LocalizerPropertySummary) .AddTo(@class); } Declare.Property(settings.ResourceManagerType, ResourceManagerPropertyName).Static( ) .Modifiers(settings.AccessModifiers) .Get(get => get.Return(resourceManager)) .AddSummary(ResourceManagerPropertySummary) .Attributed(editorBrowsable) .AddTo(@class); var cultureField = Declare.Field <CultureInfo> (CultureInfoFieldName) .Modifiers(settings.AccessModifiers & MemberAttributes.Static) .AddTo(@class); var field = @class.Instance( ).Field(CultureInfoFieldName); Declare.Property <CultureInfo> (CultureInfoPropertyName) .Modifiers(settings.AccessModifiers) .Get(get => get.Return(field)) .Set((set, value) => { set.Add(Code.If(field.ObjectEquals(value)) .Then(Code.Return( ))); set.Add(field.Assign(value)); if (notifyCultureChanged != null) { set.Add(notifyCultureChanged); } }) .AddSummary(CultureInfoPropertySummary) .Attributed(editorBrowsable) .AddTo(@class); }