本文整理汇总了C#中Emitter.TypeCastable方法的典型用法代码示例。如果您正苦于以下问题:C# Emitter.TypeCastable方法的具体用法?C# Emitter.TypeCastable怎么用?C# Emitter.TypeCastable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Emitter
的用法示例。
在下文中一共展示了Emitter.TypeCastable方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compile
public override void Compile(Emitter.Emitter emitter)
{
var fromType = Expression.GetExpressionType(emitter);
var toType = ToType;
var simpleTypes = new[] { "bool", "int", "float", "long", "complex" };
if (emitter.ResolveType(toType) == null)
Error(String.Format(Resources.errTypeNotFound, toType));
// compile the expression itself
Expression.Compile(emitter);
// idiotic case?
if (fromType == toType) return;
if(fromType == "null")
{
if(toType.IsAnyOf(simpleTypes))
Error(String.Format(Resources.errInvalidNullCast, toType));
// no actual casting is required
}
// cast simple types
else if(fromType.IsAnyOf(simpleTypes))
{
if (toType.IsAnyOf(simpleTypes))
{
switch(toType)
{
case "bool": emitter.EmitConvertToBool(); break;
case "int": emitter.EmitConvertToInt(); break;
case "float": emitter.EmitConvertToFloat(); break;
default: throw new NotImplementedException();
}
}
else
Error(String.Format(Resources.errInvalidCast, fromType, toType));
}
else
{
// complex type to simple type: search for a conversion method
if(toType.IsAnyOf(simpleTypes))
{
// to_b, to_f, to_i
string methodName = "to_" + toType[0];
MethodNode converter = null;
try
{
converter = emitter.FindMethod(fromType, methodName);
}
catch
{
Error(String.Format(Resources.errInvalidCast, fromType, toType));
}
// call the method
emitter.EmitCall(converter);
}
else
{
if (fromType.Contains("[]") || toType.Contains("[]"))
Error(Resources.errCastArray);
if(!emitter.TypeCastable(fromType, toType))
Error(String.Format(Resources.errInvalidCast, fromType, toType));
var type = emitter.FindType(toType);
emitter.EmitCast(type.Type);
}
}
}