本文整理汇总了C#中PType.CreatePValue方法的典型用法代码示例。如果您正苦于以下问题:C# PType.CreatePValue方法的具体用法?C# PType.CreatePValue怎么用?C# PType.CreatePValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PType
的用法示例。
在下文中一共展示了PType.CreatePValue方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InternalConvertTo
protected override bool InternalConvertTo(
StackContext sctx,
PValue subject,
PType target,
bool useExplicit,
out PValue result)
{
var objT = target as ObjectPType;
result = null;
if ((object)objT != null)
{
var clrType = objT.ClrType;
var genericTypeTemplate = clrType.IsGenericType ? clrType.GetGenericTypeDefinition() : null;
if (clrType == typeof(IEnumerable<PValue>) ||
clrType == typeof(List<PValue>) ||
clrType == typeof(ICollection<PValue>) ||
clrType == typeof(IList<PValue>) ||
clrType == typeof(IEnumerable) ||
clrType == typeof(ICollection) ||
clrType == typeof(IList))
result = target.CreatePValue(subject);
else if (clrType == typeof(PValue[]) && useExplicit)
result = target.CreatePValue(((List<PValue>)subject.Value).ToArray());
else if (clrType == typeof(PValueKeyValuePair))
{
var lst = (List<PValue>)subject.Value;
var key = lst.Count > 0 ? lst[0] : Null.CreatePValue();
var valueList = new List<PValue>(lst.Count > 0 ? lst.Count - 1 : 0);
for (var i = 1; i < lst.Count; i++)
valueList.Add(lst[i]);
var value = List.CreatePValue(valueList);
result = target.CreatePValue(new PValueKeyValuePair(key, value));
}
else if (clrType.IsArray)
{
//Convert each element in the list to the element type of the array.
var et = clrType.GetElementType();
var lst = (List<PValue>)subject.Value;
var array = Array.CreateInstance(et, lst.Count);
var success = true;
for (var i = 0; i < lst.Count; i++)
{
PValue converted;
if (lst[i].TryConvertTo(sctx, et, useExplicit, out converted))
{
array.SetValue(converted.Value, i);
}
else
{
success = false;
break;
}
}
if (success)
result = sctx.CreateNativePValue(array);
}
else if (genericTypeTemplate == typeof (IEnumerable<>)
|| genericTypeTemplate == typeof(ICollection<>)
|| genericTypeTemplate == typeof(IReadOnlyCollection<>)
|| genericTypeTemplate == typeof(IReadOnlyList<>)
|| genericTypeTemplate == typeof(IList<>))
{
// Convert each element in the list to the element type of the sequence
var elementT = clrType.GetGenericArguments()[0];
var listT = typeof (List<>).MakeGenericType(elementT);
// ReSharper disable once PossibleNullReferenceException
var list = (IList) listT.GetConstructor(new Type[0]).Invoke(new object[0]);
var success = true;
foreach (var pv in (List<PValue>)subject.Value)
{
PValue converted;
if (pv.TryConvertTo(sctx, elementT, useExplicit, out converted))
{
list.Add(converted.Value);
}
else
{
success = false;
break;
}
}
if (success)
{
if (genericTypeTemplate == typeof (IReadOnlyCollection<>)
|| genericTypeTemplate == typeof (IReadOnlyList<>))
{
// Wrap in readonly list view
var readonlyListT = typeof (ReadOnlyCollection<>).MakeGenericType(elementT);
var readonlyList =
// ReSharper disable once PossibleNullReferenceException
readonlyListT.GetConstructor(new[] {typeof (IList<>).MakeGenericType(elementT)})
.Invoke(new object[]{list});
result = sctx.CreateNativePValue(readonlyList);
}
else
{
result = sctx.CreateNativePValue(list);
}
}
//.........这里部分代码省略.........