本文整理汇总了C#中CodeWriter._方法的典型用法代码示例。如果您正苦于以下问题:C# CodeWriter._方法的具体用法?C# CodeWriter._怎么用?C# CodeWriter._使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeWriter
的用法示例。
在下文中一共展示了CodeWriter._方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateCode
public void GenerateCode(InterfaceDeclarationSyntax idecl, CodeWriter.CodeWriter w)
{
var iname = idecl.Identifier.ToString();
Console.WriteLine("GenerateCode: " + iname);
w._($"#region {iname}");
w._();
var namespaceScope = idecl.GetNamespaceScope();
var namespaceHandle = (string.IsNullOrEmpty(namespaceScope) == false)
? w.B($"namespace {idecl.GetNamespaceScope()}")
: null;
GenerateTrackableContainerCode(idecl, w);
GenerateTrackableContainerTrackerCode(idecl, w);
namespaceHandle?.Dispose();
w._();
w._($"#endregion");
}
示例2: EnsureSurrogateForINotificationChannel
private void EnsureSurrogateForINotificationChannel(Type callerType, CodeWriter.CodeWriter w)
{
if (_surrogateForINotificationChannelGenerated)
return;
var surrogateClassName = Utility.GetSurrogateClassName("INotificationChannel");
w._($"#region {surrogateClassName}");
w._();
var namespaceHandle = (string.IsNullOrEmpty(callerType.Namespace) == false)
? w.B($"namespace {callerType.Namespace}")
: null;
w._("[ProtoContract]");
using (w.B($"public class {surrogateClassName}"))
{
w._("[ProtoConverter]");
using (w.B($"public static {surrogateClassName} Convert(INotificationChannel value)"))
{
w._($"if (value == null) return null;");
w._($"return new {surrogateClassName}();");
}
w._("[ProtoConverter]");
using (w.B($"public static INotificationChannel Convert({surrogateClassName} value)"))
{
w._($"return null;");
}
}
namespaceHandle?.Dispose();
w._();
w._($"#endregion");
_surrogateForINotificationChannelGenerated = true;
}
示例3: GenerateCode
public void GenerateCode(Type type, CodeWriter.CodeWriter w)
{
Console.WriteLine("GenerateCode: " + type.GetSymbolDisplay(true));
if (Options.UseProtobuf && Options.UseSlimClient)
EnsureSurrogateForIRequestTarget(type, w);
w._($"#region {type.GetSymbolDisplay(true)}");
w._();
var namespaceHandle = (string.IsNullOrEmpty(type.Namespace) == false)
? w.B($"namespace {type.Namespace}")
: null;
// Collect all methods and make payload type name for each one
var baseTypes = type.GetInterfaces().Where(t => t.FullName != "Akka.Interfaced.IInterfacedActor").ToArray();
var infos = new List<Tuple<Type, List<Tuple<MethodInfo, Tuple<string, string>>>>>();
foreach (var t in new[] { type }.Concat(baseTypes))
{
var methods = GetInvokableMethods(t);
var method2PayloadTypeNameMap = GetPayloadTypeNames(t, methods);
infos.Add(Tuple.Create(t, GetPayloadTypeNames(t, methods)));
}
// Generate all
GeneratePayloadCode(type, w, infos.First().Item2);
GenerateRefCode(type, w, baseTypes, infos.ToArray());
if (Options.UseSlimClient == false)
GenerateSyncCode(type, w, baseTypes, infos.ToArray());
namespaceHandle?.Dispose();
w._();
w._($"#endregion");
}
示例4: GenerateCode
public void GenerateCode(InterfaceDeclarationSyntax idecl, CodeWriter.CodeWriter w)
{
var iname = idecl.Identifier.ToString();
Console.WriteLine("GenerateCode: " + iname);
w._($"#region {iname}");
w._();
var namespaceScope = idecl.GetNamespaceScope();
var namespaceHandle = (string.IsNullOrEmpty(namespaceScope) == false)
? w.B($"namespace {idecl.GetNamespaceScope()}")
: null;
var useProtoContract = idecl.AttributeLists.GetAttribute("ProtoContractAttribute") != null;
GenerateTrackablePocoCode(idecl, w, useProtoContract);
if (useProtoContract)
GenerateTrackablePocoSurrogateCode(idecl, w);
namespaceHandle?.Dispose();
w._();
w._($"#endregion");
}
示例5: 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
//.........这里部分代码省略.........
示例6: 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});");
//.........这里部分代码省略.........
示例7: 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});",
//.........这里部分代码省略.........
示例8: EnsureSurrogateForIRequestTarget
private void EnsureSurrogateForIRequestTarget(Type callerType, CodeWriter.CodeWriter w)
{
if (_surrogateForIActorRefGenerated)
return;
var surrogateClassName = Utility.GetSurrogateClassName("IRequestTarget");
w._($"#region {surrogateClassName}");
w._();
var namespaceHandle = (string.IsNullOrEmpty(callerType.Namespace) == false)
? w.B($"namespace {callerType.Namespace}")
: null;
w._("[ProtoContract]");
using (w.B($"public class {surrogateClassName}"))
{
w._($"[ProtoMember(1)] public int Id;");
w._($"[ProtoMember(2)] public string Address;");
w._();
w._("[ProtoConverter]");
using (w.B($"public static {surrogateClassName} Convert(IRequestTarget value)"))
{
w._($"if (value == null) return null;");
w._($"var target = ((BoundActorTarget)value);");
w._($"return new {surrogateClassName} {{ Id = target.Id, Address = target.Address }};");
}
w._("[ProtoConverter]");
using (w.B($"public static IRequestTarget Convert({surrogateClassName} value)"))
{
w._($"if (value == null) return null;");
w._($"return new BoundActorTarget(value.Id, value.Address);");
}
}
namespaceHandle?.Dispose();
w._();
w._($"#endregion");
_surrogateForIActorRefGenerated = true;
}
示例9: GenerateSyncCode
private void GenerateSyncCode(
Type type, CodeWriter.CodeWriter w, Type[] baseTypes,
Tuple<Type, List<Tuple<MethodInfo, Tuple<string, string>>>>[] typeInfos)
{
// NoReply Interface
var baseSynces = baseTypes.Select(t => Utility.GetActorSyncInterfaceName(t));
var baseSyncesInherit = baseSynces.Any() ? string.Join(", ", baseSynces) : "IInterfacedActorSync";
w._($"[AlternativeInterface(typeof({type.GetSymbolDisplay(typeless: true)}))]");
using (w.B($"public interface {Utility.GetActorSyncInterfaceName(type)}{type.GetGenericParameters()} : {baseSyncesInherit}{type.GetGenericConstraintClause()}"))
{
foreach (var m in typeInfos.First().Item2)
{
var method = m.Item1;
var parameters = method.GetParameters();
var paramStr = string.Join(", ", parameters.Select(p => p.GetParameterDeclaration(true)));
var returnType = method.ReturnType.GenericTypeArguments.FirstOrDefault();
var returnTypeLiteral = (returnType != null) ? returnType.GetSymbolDisplay(true) : "void";
w._($"{returnTypeLiteral} {method.Name}{method.GetGenericParameters()}({paramStr}){method.GetGenericConstraintClause()};");
}
}
}
示例10: GenerateRefCode
private void GenerateRefCode(
Type type, CodeWriter.CodeWriter w, Type[] baseTypes,
Tuple<Type, List<Tuple<MethodInfo, Tuple<string, string>>>>[] typeInfos)
{
// NoReply Interface
var baseNoReplys = baseTypes.Select(t => Utility.GetNoReplyInterfaceName(t));
var baseNoReplysInherit = baseNoReplys.Any() ? " : " + string.Join(", ", baseNoReplys) : "";
using (w.B($"public interface {Utility.GetNoReplyInterfaceName(type)}{type.GetGenericParameters()}{baseNoReplysInherit}{type.GetGenericConstraintClause()}"))
{
foreach (var m in typeInfos.First().Item2)
{
var method = m.Item1;
var parameters = method.GetParameters();
var paramStr = string.Join(", ", parameters.Select(p => p.GetParameterDeclaration(true)));
w._($"void {method.Name}{method.GetGenericParameters()}({paramStr}){method.GetGenericConstraintClause()};");
}
}
// ActorRef
var refClassName = Utility.GetActorRefClassName(type);
var refClassGenericName = refClassName + type.GetGenericParameters();
var noReplyInterfaceName = Utility.GetNoReplyInterfaceName(type);
var noReplyInterfaceGenericName = noReplyInterfaceName + type.GetGenericParameters();
using (w.B($"public class {refClassName}{type.GetGenericParameters()} : InterfacedActorRef, {type.GetSymbolDisplay()}, {noReplyInterfaceName}{type.GetGenericParameters()}{type.GetGenericConstraintClause()}"))
{
// InterfaceType property
w._($"public override Type InterfaceType => typeof({type.GetSymbolDisplay()});");
w._();
// Constructors
using (w.B($"public {refClassName}() : base(null)"))
{
}
using (w.B($"public {refClassName}(IRequestTarget target) : base(target)"))
{
}
using (w.B($"public {refClassName}(IRequestTarget target, IRequestWaiter requestWaiter, TimeSpan? timeout = null) : base(target, requestWaiter, timeout)"))
{
}
// With Helpers
using (w.B($"public {noReplyInterfaceGenericName} WithNoReply()"))
{
w._("return this;");
}
using (w.B($"public {refClassGenericName} WithRequestWaiter(IRequestWaiter requestWaiter)"))
{
w._($"return new {refClassGenericName}(Target, requestWaiter, Timeout);");
}
using (w.B($"public {refClassGenericName} WithTimeout(TimeSpan? timeout)"))
{
w._($"return new {refClassGenericName}(Target, RequestWaiter, timeout);");
}
// IInterface message methods
foreach (var t in typeInfos)
{
var payloadTableClassName = Utility.GetPayloadTableClassName(t.Item1) + type.GetGenericParameters();
foreach (var m in t.Item2)
{
var method = m.Item1;
var payloadTypes = m.Item2;
var parameters = method.GetParameters();
var parameterTypeNames = string.Join(", ", parameters.Select(p => p.GetParameterDeclaration(true)));
var parameterInits = string.Join(", ", parameters.Select(Utility.GetParameterAssignment));
var returnType = method.ReturnType.GenericTypeArguments.FirstOrDefault();
// Request Methods
var returnTaskType = (returnType != null) ? $"Task<{returnType.GetSymbolDisplay(true)}>" : "Task";
var prototype = $"public {returnTaskType} {method.Name}{method.GetGenericParameters()}({parameterTypeNames}){method.GetGenericConstraintClause()}";
using (w.B(prototype))
{
using (w.i("var requestMessage = new RequestMessage {", "};"))
{
w._($"InvokePayload = new {payloadTableClassName}.{payloadTypes.Item1}{method.GetGenericParameters()} {{ {parameterInits} }}");
}
if (returnType != null)
w._($"return SendRequestAndReceive<{returnType.GetSymbolDisplay(true)}>(requestMessage);");
else
w._($"return SendRequestAndWait(requestMessage);");
}
}
}
// IInterface_NoReply message methods
//.........这里部分代码省略.........
示例11: 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())
//.........这里部分代码省略.........
示例12: GenerateTrackablePocoSurrogateCode
private void GenerateTrackablePocoSurrogateCode(InterfaceDeclarationSyntax idecl, CodeWriter.CodeWriter w)
{
var sb = new StringBuilder();
var typeName = idecl.GetTypeName();
var trackableClassName = "Trackable" + typeName.Substring(1);
var className = trackableClassName + "TrackerSurrogate";
w._($"[ProtoContract]");
using (w.B($"public class {className}"))
{
// Collect properties with ProtoMemberAttribute attribute
var propertyWithTags =
idecl.GetProperties()
.Select(p => Tuple.Create(p, p.AttributeLists.GetAttribute("ProtoMemberAttribute")))
.Where(x => x.Item2 != null)
.ToArray();
// ProtoMember
foreach (var item in propertyWithTags)
{
var p = item.Item1;
w._($"[ProtoMember{item.Item2.ArgumentList}] " +
$"public EnvelopedObject<{p.Type}> {p.Identifier};");
}
w._();
// ConvertTrackerToSurrogate
w._("[ProtoConverter]");
using (w.B($"public static {className} Convert(TrackablePocoTracker<{typeName}> tracker)"))
{
w._($"if (tracker == null)",
$" return null;");
w._();
w._($"var surrogate = new {className}();");
using (w.B($"foreach(var changeItem in tracker.ChangeMap)"))
{
using (w.B($"switch (changeItem.Key.Name)"))
{
foreach (var item in propertyWithTags)
{
var p = item.Item1;
w._($"case \"{item.Item1.Identifier}\":");
w._($" surrogate.{p.Identifier} = new EnvelopedObject<{p.Type}>" +
$" {{ Value = ({p.Type})changeItem.Value.NewValue }};");
w._($" break;");
}
}
}
w._($"return surrogate;");
}
// ConvertSurrogateToTracker
w._("[ProtoConverter]");
using (w.B($"public static TrackablePocoTracker<{typeName}> Convert({className} surrogate)"))
{
w._($"if (surrogate == null)",
$" return null;");
w._();
w._($"var tracker = new TrackablePocoTracker<{typeName}>();");
foreach (var item in propertyWithTags)
{
var p = item.Item1;
w._($"if (surrogate.{p.Identifier} != null)");
w._($" tracker.ChangeMap.Add({trackableClassName}.PropertyTable.{p.Identifier}, " +
$"new TrackablePocoTracker<{typeName}>.Change {{ NewValue = surrogate.{p.Identifier}.Value }});");
}
w._($"return tracker;");
}
}
}
示例13: GeneratePayloadCode
private void GeneratePayloadCode(
Type type, CodeWriter.CodeWriter w,
List<Tuple<MethodInfo, string>> method2PayloadTypeNames)
{
w._($"[PayloadTable(typeof({type.GetSymbolDisplay(typeless: true)}), PayloadTableKind.Notification)]");
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);
w._($"typeof({m.Item2}{genericParameters}),");
}
}
}
// generate payload classes for all methods
foreach (var m in method2PayloadTypeNames)
{
var method = m.Item1;
var payloadTypeName = m.Item2;
// Invoke payload
if (Options.UseProtobuf)
w._("[ProtoContract, TypeAlias]");
using (w.B($"public class {payloadTypeName}{method.GetGenericParameters()} : IInterfacedPayload, IInvokable{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()});");
}
// Invoke
using (w.B("public void Invoke(object __target)"))
{
var parameterNames = string.Join(", ", method.GetParameters().Select(p => p.Name));
w._($"(({type.GetSymbolDisplay()})__target).{method.Name}{method.GetGenericParameters()}({parameterNames});");
}
}
}
}
}
示例14: GenerateObserverCode
private void GenerateObserverCode(
Type type, CodeWriter.CodeWriter w, Type[] baseTypes,
Tuple<Type, List<Tuple<MethodInfo, string>>>[] typeInfos)
{
var className = Utility.GetObserverClassName(type);
using (w.B($"public class {className}{type.GetGenericParameters()} : InterfacedObserver, {type.GetSymbolDisplay()}{type.GetGenericConstraintClause()}"))
{
// Constructor
using (w.B($"public {className}()",
$": base(null, 0)"))
{
}
// Constructor (INotificationChannel)
using (w.B($"public {className}(INotificationChannel channel, int observerId = 0)",
$": base(channel, observerId)"))
{
}
// Observer method messages
foreach (var t in typeInfos)
{
var payloadTableClassName = Utility.GetPayloadTableClassName(t.Item1) + type.GetGenericParameters();
foreach (var m in t.Item2)
{
var method = m.Item1;
var payloadType = m.Item2;
var parameters = method.GetParameters();
var parameterNames = string.Join(", ", parameters.Select(p => p.Name));
var parameterTypeNames = string.Join(", ", parameters.Select(p => (p.GetCustomAttribute<ParamArrayAttribute>() != null ? "params " : "") + p.ParameterType.GetSymbolDisplay(true) + " " + p.Name));
var parameterInits = string.Join(", ", parameters.Select(Utility.GetParameterAssignment));
// Request Methods
using (w.B($"public void {method.Name}{method.GetGenericParameters()}({parameterTypeNames}){method.GetGenericConstraintClause()}"))
{
w._($"var payload = new {payloadTableClassName}.{payloadType}{method.GetGenericParameters()} {{ {parameterInits} }};",
$"Notify(payload);");
}
}
}
}
// Protobuf-net specialized
if (Options.UseProtobuf)
{
var surrogateClassName = Utility.GetSurrogateClassName(type);
w._("[ProtoContract]");
using (w.B($"public class {surrogateClassName}"))
{
w._("[ProtoMember(1)] public INotificationChannel Channel;");
w._("[ProtoMember(2)] public int ObserverId;");
w._();
w._("[ProtoConverter]");
using (w.B($"public static {surrogateClassName} Convert({type.Name} value)"))
{
w._($"if (value == null) return null;");
w._($"var o = ({className})value;");
w._($"return new {surrogateClassName} {{ Channel = o.Channel, ObserverId = o.ObserverId }};");
}
w._("[ProtoConverter]");
using (w.B($"public static {type.Name} Convert({surrogateClassName} value)"))
{
w._($"if (value == null) return null;");
w._($"return new {className}(value.Channel, value.ObserverId);");
}
}
}
}