本文整理汇总了C#中Option.Map方法的典型用法代码示例。如果您正苦于以下问题:C# Option.Map方法的具体用法?C# Option.Map怎么用?C# Option.Map使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Option
的用法示例。
在下文中一共展示了Option.Map方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OptionalTests
public void OptionalTests()
{
var a = new Option<string>();
Assert.IsFalse(a.IsSet);
Assert.Throws<UnexpectedObjectException>(() => a.Unwrap());
Assert.AreEqual("Oi", a.UnwrapOr("Oi"));
var b = new Option<string>("Hello, World!");
Assert.IsTrue(b.IsSet);
Assert.AreEqual("Hello, World!", b.Unwrap());
Assert.AreEqual("Hello, World!", b.UnwrapOr("Goodbye!"));
var c = new Option<int>(5);
Assert.AreEqual(42, c.Map((x) => new Option<int>(37 + x)).Unwrap());
var d = new Option<int>();
Assert.IsFalse(d.Map((x) => new Option<int>(x)).IsSet);
Assert.AreEqual(42, d.MapOr(42, (x) => x));
Assert.AreEqual(42, d.MapOrElse((x) => x, () => c.MapOr(0, (x) => 37 + x)));
Assert.IsFalse(a.And(b).IsSet);
Assert.IsTrue(b.And(c).IsSet);
Assert.AreEqual(5, b.And(c).Unwrap());
Assert.IsTrue(a.Or(b).IsSet);
Assert.AreEqual(b.Or(a), a.Or(b));
Assert.AreEqual(5, d.Or(c).Unwrap());
Assert.AreEqual(5, c.OrElse(() => new Option<int>(42)).Unwrap());
Assert.AreEqual(42, d.OrElse(() => new Option<int>(42)).Unwrap());
}
示例2: CreateMessage
internal static RemoteMessageDTO CreateMessage(object message, ProcessId to, ProcessId sender, Message.Type type, Message.TagSpec tag, Option<SessionId> sessionId) =>
new RemoteMessageDTO
{
Type = (int)type,
Tag = (int)tag,
To = to.ToString(),
RequestId = -1,
MessageId = Guid.NewGuid(),
Sender = sender.ToString(),
ReplyTo = sender.ToString(),
ContentType = message == null
? null
: message.GetType().AssemblyQualifiedName,
Content = message == null
? null
: JsonConvert.SerializeObject(message, ActorSystemConfig.Default.JsonSerializerSettings),
SessionId = sessionId.Map(s => s.Value).IfNoneUnsafe(() => null)
};
示例3: CreateResponse
internal static RemoteMessageDTO CreateResponse(ActorResponse res, ProcessId to, ProcessId sender, Option<SessionId> sessionId) =>
new RemoteMessageDTO
{
Type = (int)Message.Type.User,
Tag = (int)Message.TagSpec.UserReply,
Child = null,
Exception = res.IsFaulted
? "RESPERR"
: null,
To = to.ToString(),
RequestId = res.RequestId,
MessageId = Guid.NewGuid(),
Sender = res.ReplyFrom.ToString(),
ReplyTo = res.ReplyTo.ToString(),
ContentType = res.Message.GetType().AssemblyQualifiedName,
Content = JsonConvert.SerializeObject(res.Message, ActorSystemConfig.Default.JsonSerializerSettings),
SessionId = sessionId.Map(s => s.Value).IfNoneUnsafe(() => null)
};
示例4: GetTargetName
private string GetTargetName(
Option<BindingConfig> bindingConfig,
TypePair typePair,
MemberInfo sourceMember,
Dictionary<string, string> targetBindings)
{
Option<string> targetName;
List<BindAttribute> binds = sourceMember.GetAttributes<BindAttribute>();
BindAttribute bind = binds.FirstOrDefault(x => x.TargetType.IsNull());
if (bind.IsNull())
{
bind = binds.FirstOrDefault(x => typePair.Target.IsAssignableFrom(x.TargetType));
}
if (bind.IsNotNull())
{
targetName = new Option<string>(bind.MemberName);
}
else
{
targetName = bindingConfig.Map(x => x.GetBindField(sourceMember.Name));
if (targetName.HasNoValue)
{
string targetMemberName;
if (targetBindings.TryGetValue(sourceMember.Name, out targetMemberName))
{
targetName = new Option<string>(targetMemberName);
}
else
{
targetName = new Option<string>(sourceMember.Name);
}
}
}
return targetName.Value;
}
示例5: IsIgnore
private bool IsIgnore(Option<BindingConfig> bindingConfig, TypePair typePair, MemberInfo sourceMember)
{
List<IgnoreAttribute> ignores = sourceMember.GetAttributes<IgnoreAttribute>();
if (ignores.Any(x => x.TargetType.IsNull()))
{
return true;
}
if (ignores.FirstOrDefault(x => typePair.Target.IsAssignableFrom(x.TargetType)).IsNotNull())
{
return true;
}
return bindingConfig.Map(x => x.IsIgnoreSourceField(sourceMember.Name)).Value;
}
示例6: Heartbeat
/// <summary>
/// If this node is part of a cluster then it updates a shared map of
/// node-names to states. This also downloads the latest map so the
/// cluster state is known locally.
/// </summary>
/// <param name="state">Current state</param>
/// <returns>Latest state from the cluster, or a map with just one item 'root'
/// in it that represents this node.</returns>
static State Heartbeat(State state, Option<ICluster> cluster) =>
cluster.Map(
c =>
{
try
{
var cutOff = DateTime.UtcNow.Add(0 * seconds - OfflineCutoff);
c.HashFieldAddOrUpdate(MembersKey, c.NodeName.Value, new ClusterNode(c.NodeName, DateTime.UtcNow, c.Role));
var newState = new State(c.GetHashFields<ProcessName, ClusterNode>(MembersKey, s => new ProcessName(s))
.Where(m => m.LastHeartbeat > cutOff));
var diffs = DiffState(state, newState);
diffs.Item1.Iter(offline => publish(state.Members[offline]));
diffs.Item2.Iter(online => publish(newState.Members[online]));
return newState;
}
catch(Exception e)
{
logErr(e);
return HeartbeatLocal(state);
}
})
.IfNone(HeartbeatLocal(state));
示例7: GetNodeName
static string GetNodeName(Option<ICluster> cluster) =>
cluster.Map(c => c.NodeName.Value).IfNone("root");