本文整理汇总了C#中IInternalContextAdapter.ICachePut方法的典型用法代码示例。如果您正苦于以下问题:C# IInternalContextAdapter.ICachePut方法的具体用法?C# IInternalContextAdapter.ICachePut怎么用?C# IInternalContextAdapter.ICachePut使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IInternalContextAdapter
的用法示例。
在下文中一共展示了IInternalContextAdapter.ICachePut方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
/// <summary>
/// invokes the method on the object passed in
/// </summary>
public override Object Execute(Object o, IInternalContextAdapter context)
{
bool isString = o.GetType() == typeof(string);
bool isDecimal = o.GetType() == typeof(decimal);
bool isPrimitive = o.GetType().IsPrimitive;
if (identifier == "to_quote" && (isString || isPrimitive || isDecimal))
{
return string.Format("\"{0}\"", EscapeDoubleQuote(o.ToString()));
}
else if (identifier == "to_squote" && (isString || isPrimitive || isDecimal))
{
return string.Format("'{0}'", EscapeSingleQuote(o.ToString()));
}
IDuck duck = o as IDuck;
if (duck != null)
{
return duck.GetInvoke(identifier);
}
IVelPropertyGet velPropertyGet = null;
try
{
Type c = o.GetType();
// first, see if we have this information cached.
IntrospectionCacheData introspectionCacheData = context.ICacheGet(this);
// if we have the cache data and the class of the object we are
// invoked with is the same as that in the cache, then we must
// be all-right. The last 'variable' is the method name, and
// that is fixed in the template :)
if (introspectionCacheData != null && introspectionCacheData.ContextData == c)
{
velPropertyGet = (IVelPropertyGet) introspectionCacheData.Thingy;
}
else
{
// otherwise, do the introspection, and cache it
velPropertyGet = runtimeServices.Uberspect.GetPropertyGet(o, identifier, uberInfo);
if (velPropertyGet != null && velPropertyGet.Cacheable)
{
introspectionCacheData = new IntrospectionCacheData(c, velPropertyGet);
context.ICachePut(this, introspectionCacheData);
}
}
}
catch(Exception e)
{
runtimeServices.Error(string.Format("ASTIdentifier.execute() : identifier = {0} : {1}", identifier, e));
}
// we have no executor... punt...
if (velPropertyGet == null)
return null;
// now try and execute. If we get a TargetInvocationException,
// throw that as the app wants to get these. If not, log and punt.
try
{
return velPropertyGet.Invoke(o);
}
catch(TargetInvocationException targetInvocationException)
{
EventCartridge ec = context.EventCartridge;
// if we have an event cartridge, see if it wants to veto
// also, let non-Exception Throwables go...
if (ec == null)
{
// no event cartridge to override. Just throw
String message = String.Format(
"Invocation of method '{0}' in {1}, template {2} Line {3} Column {4} threw an exception",
velPropertyGet.MethodName, o != null ? o.GetType().FullName : string.Empty,
uberInfo.TemplateName, uberInfo.Line, uberInfo.Column);
throw new MethodInvocationException(message, targetInvocationException.InnerException, velPropertyGet.MethodName);
}
else
{
try
{
return ec.HandleMethodException(o.GetType(), velPropertyGet.MethodName, targetInvocationException.InnerException);
}
catch(Exception)
{
String message = String.Format(
"Invocation of method '{0}' in {1}, template {2} Line {3} Column {4} threw an exception",
velPropertyGet.MethodName, o != null ? o.GetType().FullName : string.Empty,
uberInfo.TemplateName, uberInfo.Line, uberInfo.Column);
throw new MethodInvocationException(message, targetInvocationException.InnerException, velPropertyGet.MethodName);
}
//.........这里部分代码省略.........
示例2: Execute
/// <summary>
/// invokes the method. Returns null if a problem, the
/// actual return if the method returns something, or
/// an empty string "" if the method returns void
/// </summary>
public override Object Execute(Object o, IInternalContextAdapter context)
{
IDuck duck = o as IDuck;
object[] parameters = new object[paramCount];
if (duck != null)
{
EvalParameters(parameters, context);
return duck.Invoke(methodName, parameters);
}
/*
* new strategy (strategy!) for introspection. Since we want
* to be thread- as well as context-safe, we *must* do it now,
* at execution time. There can be no in-node caching,
* but if we are careful, we can do it in the context.
*/
MethodInfo method = null;
PropertyInfo property = null;
bool preparedAlready = false;
object[] methodArguments = new object[paramCount];
try
{
/*
* check the cache
*/
IntrospectionCacheData introspectionCacheData = context.ICacheGet(this);
Type c = o.GetType();
/*
* like ASTIdentifier, if we have cache information, and the
* Class of Object o is the same as that in the cache, we are
* safe.
*/
EvalParameters(parameters, context);
if (introspectionCacheData != null && introspectionCacheData.ContextData == c)
{
preparedAlready = true;
/*
* and get the method from the cache
*/
if (introspectionCacheData.Thingy is MethodInfo)
{
method = (MethodInfo) introspectionCacheData.Thingy;
methodArguments = BuildMethodArgs(method, parameters, paramArrayIndex);
}
if (introspectionCacheData.Thingy is PropertyInfo)
{
property = (PropertyInfo) introspectionCacheData.Thingy;
}
}
else
{
/*
* otherwise, do the introspection, and then
* cache it
*/
Object obj = PerformIntrospection(context, c, parameters);
if (obj is MethodInfo)
{
method = (MethodInfo) obj;
}
if (obj is PropertyInfo)
{
property = (PropertyInfo) obj;
}
if (obj != null)
{
introspectionCacheData = new IntrospectionCacheData();
introspectionCacheData.ContextData = c;
introspectionCacheData.Thingy = obj;
context.ICachePut(this, introspectionCacheData);
}
}
/*
* if we still haven't gotten the method, either we are calling
* a method that doesn't exist (which is fine...) or I screwed
* it up.
*/
if (method == null && property == null)
{
//.........这里部分代码省略.........
示例3: Execute
/// <summary>
/// invokes the method on the object passed in
/// </summary>
public override Object Execute(Object o, IInternalContextAdapter context)
{
if (identifier == "to_quote" && (o.GetType() == typeof(string) ||
o.GetType().IsPrimitive || o.GetType() == typeof(decimal)))
{
return "\"" + EscapeDoubleQuote(o.ToString()) + "\"";
}
else if (identifier == "to_squote" && (o.GetType() == typeof(string) ||
o.GetType().IsPrimitive || o.GetType() == typeof(decimal)))
{
return "'" + EscapeSingleQuote(o.ToString()) + "'";
}
IDuck duck = o as IDuck;
if (duck != null)
{
return duck.GetInvoke(identifier);
}
IVelPropertyGet vg = null;
try
{
Type c = o.GetType();
// first, see if we have this information cached.
IntrospectionCacheData icd = context.ICacheGet(this);
// if we have the cache data and the class of the object we are
// invoked with is the same as that in the cache, then we must
// be allright. The last 'variable' is the method name, and
// that is fixed in the template :)
if (icd != null && icd.ContextData == c)
{
vg = (IVelPropertyGet) icd.Thingy;
}
else
{
// otherwise, do the introspection, and cache it
vg = rsvc.Uberspect.GetPropertyGet(o, identifier, uberInfo);
if (vg != null && vg.Cacheable)
{
icd = new IntrospectionCacheData(c, vg);
context.ICachePut(this, icd);
}
}
}
catch(Exception e)
{
rsvc.Error("ASTIdentifier.execute() : identifier = " + identifier + " : " + e);
}
// we have no executor... punt...
if (vg == null)
return null;
// now try and execute. If we get a TargetInvocationException,
// throw that as the app wants to get these. If not, log and punt.
try
{
return vg.Invoke(o);
}
catch(TargetInvocationException ite)
{
EventCartridge ec = context.EventCartridge;
// if we have an event cartridge, see if it wants to veto
// also, let non-Exception Throwables go...
if (ec != null)
{
try
{
return ec.HandleMethodException(o.GetType(), vg.MethodName, ite.InnerException);
}
catch(Exception)
{
String message = String.Format(
"Invocation of method '{0}' in {1}, template {2} Line {3} Column {4} threw an exception",
vg.MethodName, o != null ? o.GetType().FullName : "",
uberInfo.TemplateName, uberInfo.Line, uberInfo.Column);
throw new MethodInvocationException(message, ite.InnerException, vg.MethodName);
}
}
else
{
// no event cartridge to override. Just throw
String message = String.Format(
"Invocation of method '{0}' in {1}, template {2} Line {3} Column {4} threw an exception",
vg.MethodName, o != null ? o.GetType().FullName : "",
uberInfo.TemplateName, uberInfo.Line, uberInfo.Column);
throw new MethodInvocationException(message, ite.InnerException, vg.MethodName);
}
//.........这里部分代码省略.........
示例4: GetIterator
/// <summary>
/// returns an Iterator to the collection in the #foreach()
/// </summary>
/// <param name="context"> current context </param>
/// <param name="node"> AST node </param>
/// <returns>Iterator to do the dataset </returns>
private IEnumerator GetIterator(IInternalContextAdapter context, INode node)
{
// get our list object, and punt if it's null.
Object listObject = node.GetChild(2).Value(context);
// if we have an event cartridge, get a new value object
NVelocity.App.Events.EventCartridge eventCartridge = context.EventCartridge;
if (eventCartridge != null)
{
listObject = eventCartridge.ReferenceInsert(new Stack(), node.GetChild(2).Literal, listObject);
}
if (listObject == null)
{
return null;
}
// See if we already know what type this is.
// Use the introspection cache
EnumType type = EnumType.Unknown;
IntrospectionCacheData introspectionCacheData = context.ICacheGet(this);
Type c = listObject.GetType();
// if we have an entry in the cache, and the Class we have
// cached is the same as the Class of the data object
// then we are ok
if (introspectionCacheData != null && introspectionCacheData.ContextData == c)
{
// dig the type out of the data object
type = ((EnumType)introspectionCacheData.Thingy);
}
// If we still don't know what this is,
// figure out what type of object the list
// element is, and get the iterator for it
if (type == EnumType.Unknown)
{
if (listObject.GetType().IsArray)
{
type = EnumType.Array;
}
else if (listObject is IDictionary)
{
type = EnumType.Dictionary;
}
else if (listObject is ICollection)
{
type = EnumType.Collection;
}
else if (listObject is IEnumerable)
{
type = EnumType.Enumerable;
}
else if (listObject is IEnumerator)
{
type = EnumType.Enumeration;
}
// if we did figure it out, cache it
if (type != EnumType.Unknown)
{
introspectionCacheData = new IntrospectionCacheData(c, type);
context.ICachePut(this, introspectionCacheData);
}
}
// now based on the type from either cache or examination...
switch (type)
{
case EnumType.Collection:
return ((ICollection)listObject).GetEnumerator();
case EnumType.Enumerable:
return ((IEnumerable)listObject).GetEnumerator();
case EnumType.Enumeration:
runtimeServices.Warn(
string.Format(
"Warning! The reference {0} is an Enumeration in the #foreach() loop at [{1},{2}] in template {3}. Because it's not resetable, if used in more than once, this may lead to unexpected results.",
node.GetChild(2).FirstToken.Image, Line, Column, context.CurrentTemplateName));
return (IEnumerator)listObject;
case EnumType.Array:
return ((Array)listObject).GetEnumerator();
case EnumType.Dictionary:
return ((IDictionary)listObject).GetEnumerator();
default:
/* we have no clue what this is */
runtimeServices.Warn(
string.Format(
//.........这里部分代码省略.........
示例5: GetIterator
/// <summary>
/// returns an Iterator to the collection in the #foreach()
/// </summary>
/// <param name="context"> current context </param>
/// <param name="node"> AST node </param>
/// <returns>Iterator to do the dataset </returns>
private IEnumerator GetIterator(IInternalContextAdapter context, INode node)
{
// get our list object, and punt if it's null.
Object listObject = node.GetChild(2).Value(context);
if (listObject == null)
return null;
// See if we already know what type this is.
// Use the introspection cache
EnumType type = EnumType.Unknown;
IntrospectionCacheData icd = context.ICacheGet(this);
Type c = listObject.GetType();
// if we have an entry in the cache, and the Class we have
// cached is the same as the Class of the data object
// then we are ok
if (icd != null && icd.ContextData == c)
{
// dig the type out of the cata object
type = ((EnumType) icd.Thingy);
}
// If we still don't know what this is,
// figure out what type of object the list
// element is, and get the iterator for it
if (type == EnumType.Unknown)
{
if (listObject.GetType().IsArray)
type = EnumType.Array;
else if (listObject is IDictionary)
type = EnumType.Dictionary;
else if (listObject is ICollection)
type = EnumType.Collection;
else if (listObject is IEnumerable)
type = EnumType.Enumerable;
else if (listObject is IEnumerator)
type = EnumType.Enumeration;
// if we did figure it out, cache it
if (type != EnumType.Unknown)
{
icd = new IntrospectionCacheData(c, type);
context.ICachePut(this, icd);
}
}
// now based on the type from either cache or examination...
switch(type)
{
case EnumType.Collection:
return ((ICollection) listObject).GetEnumerator();
case EnumType.Enumerable:
return ((IEnumerable) listObject).GetEnumerator();
case EnumType.Enumeration:
rsvc.Warn("Warning! The reference " + node.GetChild(2).FirstToken.Image +
" is an Enumeration in the #foreach() loop at [" + Line + "," + Column + "]" + " in template " +
context.CurrentTemplateName + ". Because it's not resetable," +
" if used in more than once, this may lead to" + " unexpected results.");
return (IEnumerator) listObject;
case EnumType.Array:
return ((Array) listObject).GetEnumerator();
case EnumType.Dictionary:
return ((IDictionary) listObject).GetEnumerator();
default:
/* we have no clue what this is */
rsvc.Warn("Could not determine type of enumerator (" + listObject.GetType().Name + ") in " + "#foreach loop for " +
node.GetChild(2).FirstToken.Image + " at [" + Line + "," + Column + "]" + " in template " +
context.CurrentTemplateName);
return null;
}
}