当前位置: 首页>>代码示例>>C#>>正文


C# CodeWriter.b方法代码示例

本文整理汇总了C#中CodeWriter.b方法的典型用法代码示例。如果您正苦于以下问题:C# CodeWriter.b方法的具体用法?C# CodeWriter.b怎么用?C# CodeWriter.b使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CodeWriter的用法示例。


在下文中一共展示了CodeWriter.b方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GenerateTrackableContainerCode

        private void GenerateTrackableContainerCode(InterfaceDeclarationSyntax idecl, CodeWriter.CodeWriter w)
        {
            var typeName = idecl.GetTypeName();
            var className = "Trackable" + typeName.Substring(1);

            var useProtoContract = idecl.AttributeLists.GetAttribute("ProtoContractAttribute") != null;
            if (useProtoContract)
                w._($"[ProtoContract]");

            using (w.B($"public partial class {className} : {typeName}"))
            {
                var properties = idecl.GetProperties();

                // Tracker

                w._($"[IgnoreDataMember]",
                    $"private {className}Tracker _tracker;");
                w._();

                w._($"[IgnoreDataMember]");
                using (w.B($"public {className}Tracker Tracker"))
                {
                    using (w.b($"get"))
                    {
                        w._($"return _tracker;");
                    }
                    using (w.b($"set"))
                    {
                        w._($"_tracker = value;");
                        foreach (var p in properties)
                        {
                            var propertyName = p.Identifier.ToString();
                            w._($"{propertyName}.Tracker = value?.{propertyName}Tracker;");
                        }
                    }
                }

                // Clone

                using (w.B($"public {className} Clone()"))
                {
                    w._($"var o = new {className}();");
                    foreach (var p in properties)
                    {
                        w._($"o._{p.Identifier} = _{p.Identifier}?.Clone();");
                    }
                    w._($"return o;");
                }

                // ITrackable.Changed

                w._("public bool Changed { get { return Tracker != null && Tracker.HasChange; } }");
                w._();

                // ITrackable.Tracker

                using (w.B($"ITracker ITrackable.Tracker"))
                {
                    using (w.b($"get"))
                    {
                        w._($"return Tracker;");
                    }
                    using (w.b($"set"))
                    {
                        w._($"var t = ({className}Tracker)value;");
                        w._($"Tracker = t;");
                    }
                }

                // ITrackable<T>.Tracker

                using (w.B($"ITracker<{typeName}> ITrackable<{typeName}>.Tracker"))
                {
                    using (w.b($"get"))
                    {
                        w._($"return Tracker;");
                    }
                    using (w.b($"set"))
                    {
                        w._($"var t = ({className}Tracker)value;");
                        w._($"Tracker = t;");
                    }
                }

                // IContainerTracker<T>.Tracker

                using (w.B($"IContainerTracker<{typeName}> ITrackableContainer<{typeName}>.Tracker"))
                {
                    using (w.b($"get"))
                    {
                        w._($"return Tracker;");
                    }
                    using (w.b($"set"))
                    {
                        w._($"var t = ({className}Tracker)value;");
                        w._($"Tracker = t;");
                    }
                }

                // ITrackable.Clone
//.........这里部分代码省略.........
开发者ID:SaladLab,项目名称:TrackableData,代码行数:101,代码来源:TrackableContainerCodeGenerator.cs

示例2: GeneratePayloadCode

        private void GeneratePayloadCode(
            Type type, CodeWriter.CodeWriter w,
            List<Tuple<MethodInfo, Tuple<string, string>>> method2PayloadTypeNames)
        {
            var tagName = Utility.GetActorInterfaceTagName(type);

            w._($"[PayloadTable(typeof({type.GetSymbolDisplay(typeless: true)}), PayloadTableKind.Request)]");
            using (w.B($"public static class {Utility.GetPayloadTableClassName(type)}{type.GetGenericParameters()}{type.GetGenericConstraintClause()}"))
            {
                // generate GetPayloadTypes method

                using (w.B("public static Type[,] GetPayloadTypes()"))
                {
                    using (w.i("return new Type[,] {", "};"))
                    {
                        foreach (var m in method2PayloadTypeNames)
                        {
                            var genericParameters = m.Item1.GetGenericParameters(typeless: true);
                            var payloadTypes = m.Item2;
                            var returnType = payloadTypes.Item2 != "" ? $"typeof({payloadTypes.Item2}{genericParameters})" : "null";
                            w._($"{{ typeof({payloadTypes.Item1}{genericParameters}), {returnType} }},");
                        }
                    }
                }

                // generate payload classes for all methods

                foreach (var m in method2PayloadTypeNames)
                {
                    var method = m.Item1;
                    var payloadTypes = m.Item2;
                    var returnType = method.ReturnType.GenericTypeArguments.FirstOrDefault();
                    var observerParameters = method.GetParameters()
                        .Select(p => Tuple.Create(p, Utility.GetReachableMemebers(p.ParameterType, Utility.IsObserverInterface).ToArray()))
                        .Where(i => i.Item2.Length > 0)
                        .ToArray();

                    // Invoke payload

                    if (Options.UseProtobuf)
                        w._("[ProtoContract, TypeAlias]");

                    var tagOverridable = tagName != null ? ", IPayloadTagOverridable" : "";
                    var observerUpdatable = observerParameters.Any() ? ", IPayloadObserverUpdatable" : "";
                    using (w.B($"public class {payloadTypes.Item1}{method.GetGenericParameters()}",
                               $": IInterfacedPayload, IAsyncInvokable{tagOverridable}{observerUpdatable}{method.GetGenericConstraintClause()}"))
                    {
                        // Parameters

                        var parameters = method.GetParameters();
                        for (var i = 0; i < parameters.Length; i++)
                        {
                            var parameter = parameters[i];

                            var attr = "";
                            var defaultValueExpression = "";
                            if (Options.UseProtobuf)
                            {
                                var defaultValueAttr =
                                    parameter.HasNonTrivialDefaultValue()
                                        ? $", DefaultValue({parameter.DefaultValue.GetValueLiteral()})"
                                        : "";
                                attr = $"[ProtoMember({i + 1}){defaultValueAttr}] ";

                                if (parameter.HasNonTrivialDefaultValue())
                                {
                                    defaultValueExpression = " = " + parameter.DefaultValue.GetValueLiteral();
                                }
                            }

                            var typeName = parameter.ParameterType.GetSymbolDisplay(true);
                            w._($"{attr}public {typeName} {parameter.Name}{defaultValueExpression};");
                        }
                        if (parameters.Any())
                            w._();

                        // GetInterfaceType

                        using (w.B($"public Type GetInterfaceType()"))
                        {
                            w._($"return typeof({type.GetSymbolDisplay()});");
                        }

                        // InvokeAsync

                        if (Options.UseSlimClient)
                        {
                            using (w.B("public Task<IValueGetable> InvokeAsync(object __target)"))
                            {
                                w._("return null;");
                            }
                        }
                        else
                        {
                            using (w.B("public async Task<IValueGetable> InvokeAsync(object __target)"))
                            {
                                var parameterNames = string.Join(", ", method.GetParameters().Select(p => p.Name));
                                if (returnType != null)
                                {
                                    w._($"var __v = await (({type.GetSymbolDisplay()})__target).{method.Name}{method.GetGenericParameters()}({parameterNames});",
//.........这里部分代码省略.........
开发者ID:SaladLab,项目名称:Akka.Interfaced,代码行数:101,代码来源:InterfacedActorCodeGenerator.cs

示例3: GenerateTrackableContainerTrackerCode

        private void GenerateTrackableContainerTrackerCode(InterfaceDeclarationSyntax idecl, CodeWriter.CodeWriter w)
        {
            var typeName = idecl.GetTypeName();
            var className = "Trackable" + typeName.Substring(1) + "Tracker";

            var useProtoContract = idecl.AttributeLists.GetAttribute("ProtoContractAttribute") != null;
            if (useProtoContract)
                w._($"[ProtoContract]");

            using (w.B($"public class {className} : IContainerTracker<{typeName}>"))
            {
                // Property Trackers

                var properties = idecl.GetProperties();
                foreach (var p in properties)
                {
                    var protoMemberAttr = p.AttributeLists.GetAttribute("ProtoMemberAttribute");
                    if (protoMemberAttr != null)
                        w._($"[ProtoMember{protoMemberAttr.ArgumentList}] ");

                    var propertyName = p.Identifier.ToString();
                    var trackerName = Utility.GetTrackerClassName(p.Type);
                    w._($"public {trackerName} {propertyName}Tracker {{ get; set; }} = new {trackerName}();");
                }
                w._();

                // ToString()

                using (w.B($"public override string ToString()"))
                {
                    w._("var sb = new StringBuilder();",
                        "sb.Append(\"{ \");",
                        "var first = true;");
                    foreach (var p in properties)
                    {
                        var propertyName = p.Identifier.ToString();
                        using (w.B($"if ({propertyName}Tracker != null && {propertyName}Tracker.HasChange)"))
                        {
                            w._($"if (first)",
                                $"    first = false;",
                                $"else",
                                $"    sb.Append(\", \");",
                                $"sb.Append(`{propertyName}:`);",
                                $"sb.Append({propertyName}Tracker);");
                        }
                    }
                    w._("sb.Append(\" }\");",
                        "return sb.ToString();");
                }

                // ITracker.HasChange

                using (w.B($"public bool HasChange"))
                {
                    using (w.b($"get"))
                    {
                        w._($"return");
                        foreach (var p in properties)
                        {
                            var propertyName = p.Identifier.ToString();
                            w._($"    ({propertyName}Tracker != null && {propertyName}Tracker.HasChange) ||");
                        }
                        w._($"    false;");
                    }
                }

                // ITracker.HasChange

                using (w.B($"public event TrackerHasChangeSet HasChangeSet"))
                {
                    w._("add { throw new NotImplementedException(); }",
                        "remove { throw new NotImplementedException(); }");
                }

                // ITracker.Clear

                using (w.B($"public void Clear()"))
                {
                    foreach (var p in properties)
                    {
                        var propertyName = p.Identifier.ToString();
                        w._($"if ({propertyName}Tracker != null)",
                            $"    {propertyName}Tracker.Clear();");
                    }
                }

                // ITracker.ApplyTo(Trackable)

                using (w.B($"public void ApplyTo(object trackable)"))
                {
                    w._($"ApplyTo(({typeName})trackable);");
                }

                using (w.B($"public void ApplyTo({typeName} trackable)"))
                {
                    foreach (var p in properties)
                    {
                        var propertyName = p.Identifier.ToString();
                        w._($"if ({propertyName}Tracker != null)",
                            $"    {propertyName}Tracker.ApplyTo(trackable.{propertyName});");
//.........这里部分代码省略.........
开发者ID:SaladLab,项目名称:TrackableData,代码行数:101,代码来源:TrackableContainerCodeGenerator.cs

示例4: GenerateTrackablePocoCode

        private void GenerateTrackablePocoCode(InterfaceDeclarationSyntax idecl, CodeWriter.CodeWriter w,
                                               bool useProtoContract)
        {
            var typeName = idecl.GetTypeName();
            var className = "Trackable" + typeName.Substring(1);

            var properties = idecl.GetProperties();
            var trackableProperties = Utility.GetTrackableProperties(properties);

            if (useProtoContract)
                w._($"[ProtoContract]");

            using (w.B($"public partial class {className} : {typeName}"))
            {
                // Tracker

                w._($"[IgnoreDataMember]",
                    $"public IPocoTracker<{typeName}> Tracker {{ get; set; }}");
                w._();

                // Clone

                using (w.B($"public {className} Clone()"))
                {
                    w._($"var o = new {className}();");
                    foreach (var p in properties)
                    {
                        if (Utility.IsTrackableType(p.Type))
                            w._($"o._{p.Identifier} = _{p.Identifier}?.Clone();");
                        else
                            w._($"o._{p.Identifier} = _{p.Identifier};");
                    }
                    w._($"return o;");
                }

                // ITrackable.Changed

                w._("[IgnoreDataMember]",
                    "public bool Changed { get { return Tracker != null && Tracker.HasChange; } }");
                w._();

                // ITrackable.Tracker

                using (w.B($"ITracker ITrackable.Tracker"))
                {
                    using (w.b($"get"))
                    {
                        w._($"return Tracker;");
                    }
                    using (w.b($"set"))
                    {
                        w._($"var t = (IPocoTracker<{typeName}>)value;",
                            $"Tracker = t;");
                    }
                }

                // ITrackable<T>.Tracker

                using (w.B($"ITracker<{typeName}> ITrackable<{typeName}>.Tracker"))
                {
                    using (w.b($"get"))
                    {
                        w._($"return Tracker;");
                    }
                    using (w.b($"set"))
                    {
                        w._($"var t = (IPocoTracker<{typeName}>)value;",
                            $"Tracker = t;");
                    }
                }

                // ITrackable.Clone

                using (w.B($"ITrackable ITrackable.Clone()"))
                {
                    w._($"return Clone();");
                }

                // ITrackable.GetChildTrackable

                using (w.B($"public ITrackable GetChildTrackable(object name)"))
                {
                    using (w.B($"switch ((string)name)"))
                    {
                        foreach (var p in trackableProperties)
                        {
                            w._($"case \"{p.Identifier}\":",
                                $"    return {p.Identifier} as ITrackable;");
                        }
                        w._($"default:",
                            $"    return null;");
                    }
                }

                // ITrackable.GetChildTrackables

                using (w.B($"public IEnumerable<KeyValuePair<object, ITrackable>> " + $"" +
                           $"GetChildTrackables(bool changedOnly = false)"))
                {
                    if (trackableProperties.Any())
//.........这里部分代码省略.........
开发者ID:SaladLab,项目名称:TrackableData,代码行数:101,代码来源:TrackablePocoCodeGenerator.cs


注:本文中的CodeWriter.b方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。