本文整理汇总了C#中Context.Lookup方法的典型用法代码示例。如果您正苦于以下问题:C# Context.Lookup方法的具体用法?C# Context.Lookup怎么用?C# Context.Lookup使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Context
的用法示例。
在下文中一共展示了Context.Lookup方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Render
/// <summary>
/// Renders a tokens representation (or interpolated lambda result) escaping the
/// result
/// </summary>
/// <param name="writer">The writer to write the token to</param>
/// <param name="context">The context to discover values from</param>
/// <param name="partials">The partial templates available to the token</param>
/// <param name="originalTemplate">The original template</param>
/// <returns>The rendered token result that has been escaped</returns>
public string Render(Writer writer, Context context, IDictionary<string, string> partials, string originalTemplate)
{
var value = context.Lookup(Value);
value = InterpolateLambdaValueIfPossible(value, writer, context, partials);
return value != null ? WebUtility.HtmlEncode(value.ToString()) : null;
}
示例2: Render
/// <summary>
/// Renders a tokens representation if the value (or interpolated value result)
/// is truthy
/// </summary>
/// <param name="writer">The writer to write the token to</param>
/// <param name="context">The context to discover values from</param>
/// <param name="partials">The partial templates available to the token</param>
/// <param name="originalTemplate">The original template</param>
/// <returns>The rendered result of the token if the resolved value is not truthy</returns>
public string Render(Writer writer, Context context, IDictionary<string, string> partials, string originalTemplate)
{
var value = context.Lookup(Value);
value = InterpolateLambdaValueIfPossible(value, writer, context, partials);
return !context.IsTruthyValue(value) ? writer.RenderTokens(ChildTokens, context, partials, originalTemplate) : null;
}
示例3: Render
/// <summary>
/// Renders a tokens representation (or interpolated lambda result)
/// </summary>
/// <param name="writer">The writer to write the token to</param>
/// <param name="context">The context to discover values from</param>
/// <param name="partials">The partial templates available to the token</param>
/// <param name="originalTemplate">The original template</param>
/// <returns>The rendered token result</returns>
public string Render(Writer writer, Context context, IDictionary<string, string> partials, string originalTemplate)
{
var value = context.Lookup(Value);
value = InterpolateLambdaValueIfPossible(value, writer, context, partials);
return value?.ToString();
}
示例4: TestNullValueDoesntAlterDictionary
public void TestNullValueDoesntAlterDictionary()
{
var copyField = new Scalar(new QName("value"), FastType.I32, Operator.Copy, new IntegerValue(10), true);
var field = new Scalar(new QName("value"), FastType.I32, Operator.Default, new IntegerValue(10), true);
MessageTemplate copyTemplate = Template(copyField);
MessageTemplate template = Template(field);
var context = new Context();
var encoder = new FastEncoder(context);
encoder.RegisterTemplate(1, template);
encoder.RegisterTemplate(2, copyTemplate);
var message = (Message)copyTemplate.CreateValue(null);
message.SetInteger(1, 11);
encoder.Encode(message);
Assert.AreEqual(11, context.Lookup("global", copyTemplate, new QName("value")).ToInt());
message = (Message)template.CreateValue(null);
encoder.Encode(message);
Assert.AreEqual(11, context.Lookup("global", copyTemplate, new QName("value")).ToInt());
}
示例5: TestLookup
public void TestLookup()
{
var allocationInstruction =
new MessageTemplate(ObjectMother.AllocationInstruction)
{
TypeReference = new QName("AllocationInstruction")
};
var allocations = new Sequence(ObjectMother.Allocations) {TypeReference = new QName("Allocation")};
var context = new Context();
context.Store(DictionaryFields.Type, allocationInstruction, new QName("ID"), String("1234"));
Assert.AreEqual(String("1234"),
context.Lookup(DictionaryFields.Type, allocationInstruction, new QName("ID")));
Assert.AreEqual(ScalarValue.Undefined,
context.Lookup(DictionaryFields.Type, allocations.Group, new QName("ID")));
}
示例6: Render
/// <summary>
/// Returns the rendered result of all the child tokens of the section
/// if the token value is truthy
/// </summary>
/// <param name="writer">The writer to write the token to</param>
/// <param name="context">The context to discover values from</param>
/// <param name="partials">The partial templates available to the token</param>
/// <param name="originalTemplate">The original template</param>
/// <returns>The rendered result of all the sections children</returns>
public string Render(Writer writer, Context context, IDictionary<string, string> partials, string originalTemplate)
{
var buffer = new StringBuilder();
var value = context.Lookup(Value);
if (!context.IsTruthyValue(value))
{
return null;
}
if (value is IEnumerable && !EnumerableBlacklist.Any(x => x.IsInstanceOfType(value)))
{
var arrayValue = value as IEnumerable;
foreach (var v in arrayValue)
{
buffer.Append(writer.RenderTokens(ChildTokens, context.Push(v), partials, originalTemplate));
}
}
else if (value is IEnumerator)
{
var enumeratorValue = value as IEnumerator;
while (enumeratorValue.MoveNext())
{
buffer.Append(writer.RenderTokens(ChildTokens, context.Push(enumeratorValue.Current), partials, originalTemplate));
}
}
else if (value is Func<dynamic, string, object> || value is Func<string, object>)
{
var functionDynamicValue = value as Func<dynamic, string, object>;
var functionStringValue = value as Func<string, object>;
var sectionContent = originalTemplate.Slice(End, ParentSectionEnd);
value = functionDynamicValue != null ? functionDynamicValue.Invoke(context.View, sectionContent) : functionStringValue.Invoke(sectionContent);
value = writer.Render(value.ToString(), context, partials, Tags);
if (value != null)
{
buffer.Append(value);
}
}
else if (value is IDictionary || value != null)
{
buffer.Append(writer.RenderTokens(ChildTokens, context.Push(value), partials, originalTemplate));
}
return buffer.ToString();
}
示例7: Encode
public override byte[] Encode(FieldValue fieldValue, Group encodeTemplate, Context context, BitVectorBuilder presenceMapBuilder)
{
var priorValue = context.Lookup(Dictionary, encodeTemplate, Key);
var value_Renamed = (ScalarValue) fieldValue;
if (!operatorCodec.CanEncode(value_Renamed, this))
{
Global.HandleError(Error.FastConstants.D3_CANT_ENCODE_VALUE, "The scalar " + this + " cannot encode the value " + value_Renamed);
}
var valueToEncode = operatorCodec.GetValueToEncode(value_Renamed, priorValue, this, presenceMapBuilder);
if (operator_Renamed.ShouldStoreValue(value_Renamed))
{
context.Store(Dictionary, encodeTemplate, Key, value_Renamed);
}
if (valueToEncode == null)
{
return new byte[0];
}
byte[] encoding = typeCodec.Encode(valueToEncode);
if (context.TraceEnabled && encoding.Length > 0)
{
context.GetEncodeTrace().Field(this, fieldValue, valueToEncode, encoding, presenceMapBuilder.Index);
}
return encoding;
}
示例8: Encode
public override byte[] Encode(IFieldValue fieldValue, Group encodeTemplate, Context context,
BitVectorBuilder presenceMapBuilder)
{
IDictionary dict = context.GetDictionary(Dictionary);
ScalarValue priorValue = context.Lookup(dict, encodeTemplate, Key);
var value = (ScalarValue) fieldValue;
if (!_operatorCodec.CanEncode(value, this))
{
Global.ErrorHandler.OnError(null, DynError.CantEncodeValue,
"The scalar {0} cannot encode the value {1}", this, value);
}
ScalarValue valueToEncode = _operatorCodec.GetValueToEncode(value, priorValue, this,
presenceMapBuilder);
if (_operator.ShouldStoreValue(value))
{
context.Store(dict, encodeTemplate, Key, value);
}
if (valueToEncode == null)
{
return ByteUtil.EmptyByteArray;
}
byte[] encoding = _typeCodec.Encode(valueToEncode);
if (context.TraceEnabled && encoding.Length > 0)
{
context.EncodeTrace.Field(this, fieldValue, valueToEncode, encoding, presenceMapBuilder.Index);
}
return encoding;
}
示例9: Decode
public override FieldValue Decode(System.IO.Stream in_Renamed, Group decodeTemplate, Context context, BitVectorReader presenceMapReader)
{
try
{
ScalarValue previousValue = null;
if (operator_Renamed.UsesDictionary())
{
previousValue = context.Lookup(Dictionary, decodeTemplate, Key);
ValidateDictionaryTypeAgainstFieldType(previousValue, type);
}
ScalarValue value_Renamed;
int pmapIndex = presenceMapReader.Index;
if (IsPresent(presenceMapReader))
{
if (context.TraceEnabled)
in_Renamed = new RecordingInputStream(in_Renamed);
if (!operatorCodec.ShouldDecodeType())
{
return operatorCodec.DecodeValue(null, null, this);
}
ScalarValue decodedValue = typeCodec.Decode(in_Renamed);
value_Renamed = DecodeValue(decodedValue, previousValue);
if (context.TraceEnabled)
context.DecodeTrace.Field(this, value_Renamed, decodedValue, ((RecordingInputStream) in_Renamed).Buffer, pmapIndex);
}
else
{
value_Renamed = Decode(previousValue);
}
ValidateDecodedValueIsCorrectForType(value_Renamed, type);
if (!((Operator == Template.Operator.Operator.DELTA) && (value_Renamed == null)))
{
context.Store(Dictionary, decodeTemplate, Key, value_Renamed);
}
return value_Renamed;
}
catch (FastException e)
{
throw new FastException("Error occurred while decoding " + this, e.Code, e);
}
}
示例10: Propogate
public override void Propogate(Context env, object matched, double strength)
{
base.Propogate(env, matched, strength);
if (strength > .5 && matched is IParsedPhrase)
((Variable)env.Lookup("%verb")).Propogate(env, new WordPhrase(verbs.InputToBase(((IParsedPhrase) matched).Text), "VB"), .5 * strength);
}
示例11: Decode
public override IFieldValue Decode(Stream inStream, Group decodeTemplate, Context context,
BitVectorReader presenceMapReader)
{
try
{
ScalarValue priorValue = null;
IDictionary dict = null;
QName key = Key;
ScalarValue value;
int pmapIndex = presenceMapReader.Index;
if (IsPresent(presenceMapReader))
{
if (context.TraceEnabled)
inStream = new RecordingInputStream(inStream);
if (!_operatorCodec.ShouldDecodeType)
return _operatorCodec.DecodeValue(null, null, this);
if (_operatorCodec.DecodeNewValueNeedsPrevious)
{
dict = context.GetDictionary(Dictionary);
priorValue = context.Lookup(dict, decodeTemplate, key);
ValidateDictionaryTypeAgainstFieldType(priorValue, _fastType);
}
ScalarValue decodedValue = _typeCodec.Decode(inStream);
value = _operatorCodec.DecodeValue(decodedValue, priorValue, this);
if (context.TraceEnabled)
context.DecodeTrace.Field(this, value, decodedValue,
((RecordingInputStream) inStream).Buffer, pmapIndex);
}
else
{
if (_operatorCodec.DecodeEmptyValueNeedsPrevious)
{
dict = context.GetDictionary(Dictionary);
priorValue = context.Lookup(dict, decodeTemplate, key);
ValidateDictionaryTypeAgainstFieldType(priorValue, _fastType);
}
value = _operatorCodec.DecodeEmptyValue(priorValue, this);
}
ValidateDecodedValueIsCorrectForType(value, _fastType);
#warning TODO: Review if this previous "if" statement is needed.
// if (Operator != Template.Operator.Operator.DELTA || value != null)
if (value != null &&
(_operatorCodec.DecodeNewValueNeedsPrevious || _operatorCodec.DecodeEmptyValueNeedsPrevious))
{
context.Store(dict ?? context.GetDictionary(Dictionary), decodeTemplate, key, value);
}
return value;
}
catch (DynErrorException e)
{
throw new DynErrorException(e, e.Error, "Error occurred while decoding {0}", this);
}
}
示例12: RenderSection
string RenderSection(Token token, Context context, object partials, string originalTemplate)
{
var buffer = "";
var value = context.Lookup(token.Value);
// This function is used to Render an arbitrary template
// in the current context by higher-order sections.
Func<string, string> subRender = template => Render(template, context, partials);
if (value == null || value is bool && !(bool) value)
return ""; // this would have returned null/undefined instead of a string in JS version?
//if (IsArray(Value)) {
// TODO: HORRIBLE HACK to differentiate Jobject and iterables.
if ((value.GetType().ToString().Contains("Jobject")))
{
buffer += RenderTokens(token.SubTokens, context.Push(value), partials, originalTemplate);
}
else if (IsArray(value))
{
foreach (var item in (IList) value)
{
buffer += RenderTokens(token.SubTokens, context.Push(item), partials, originalTemplate);
}
//} else if (typeof Value === 'object' || typeof Value === 'string' || typeof Value === 'number') {
// buffer += this.RenderTokens(token[4], context.Push(Value), partials, originalTemplate);
}
else if (value is Func<string, Func<string, string>, string>)
{
var lambda =
value as Func<string, Func<string, string>, string>;
//Value = lambda(context._view, originalTemplate.Substring(token.End, token.EndSection), subRender);
// TODO: how do we set the "this" in c#? Do we care?
value = lambda(originalTemplate.Substring(token.End, token.EndSection - token.End), subRender);
if (value != null)
buffer += value;
//} else if (Value as dynamic != null) {
// buffer += this.RenderTokens(token.SubTokens, context.Push(Value), partials, originalTemplate);
}
else
{
buffer += RenderTokens(token.SubTokens, context, partials, originalTemplate);
}
return buffer;
}
示例13: UnescapedValue
string UnescapedValue(Token token, Context context)
{
var value = context.Lookup(token.Value);
return (string) value;
}
示例14: EscapedValue
string EscapedValue(Token token, Context context)
{
var value = context.Lookup(token.Value);
if (value == null) return null;
var str = token.Format != null ? ((DateTime) value).ToString(token.Format) : value.ToString();
return SecurityElement.Escape(str);
}
示例15: RenderInverted
string RenderInverted(Token token, Context context, object partials, string originalTemplate)
{
var value = context.Lookup(token.Value);
// Use JavaScript's definition of falsy. Include empty arrays.
// See https://github.com/janl/mustache.js/issues/186
//if (!Value || (IsArray(Value) && Value.length === 0))
if (value == null || IsEmptyArray(value))
return RenderTokens(token.SubTokens, context, partials, originalTemplate);
return null; // TODO?
}