本文整理汇总了C#中JSExpression.GetExpectedType方法的典型用法代码示例。如果您正苦于以下问题:C# JSExpression.GetExpectedType方法的具体用法?C# JSExpression.GetExpectedType怎么用?C# JSExpression.GetExpectedType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JSExpression
的用法示例。
在下文中一共展示了JSExpression.GetExpectedType方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Cast
public JSExpression Cast(JSExpression expression, TypeReference targetType)
{
var currentType = ILBlockTranslator.DereferenceType(expression.GetExpectedType(TypeSystem));
targetType = ILBlockTranslator.DereferenceType(targetType);
if (targetType.MetadataType == MetadataType.Char) {
return JSInvocationExpression.InvokeStatic(JS.fromCharCode, new[] { expression }, true);
} else if (
(currentType.MetadataType == MetadataType.Char) &&
ILBlockTranslator.IsIntegral(targetType)
) {
return JSInvocationExpression.InvokeMethod(JS.charCodeAt, expression, new[] { JSLiteral.New(0) }, true);
} else if (
ILBlockTranslator.IsEnum(currentType) &&
ILBlockTranslator.IsIntegral(targetType)
) {
return new JSDotExpression(
expression, new JSStringIdentifier("value", targetType)
);
} else {
return JSInvocationExpression.InvokeStatic(
Dot("Cast", targetType),
new [] { expression, new JSType(targetType) }, true
);
}
}
示例2: IsCopyNeeded
protected bool IsCopyNeeded(JSExpression value)
{
if ((value == null) || (value.IsNull))
return false;
var valueType = value.GetExpectedType(TypeSystem);
if (!IsStruct(valueType))
return false;
if (
(value is JSLiteral) ||
(value is JSInvocationExpression) ||
(value is JSDelegateInvocationExpression) ||
(value is JSNewExpression) ||
(value is JSPassByReferenceExpression)
) {
return false;
}
var rightVar = value as JSVariable;
if (rightVar != null) {
int referenceCount;
if (ReferenceCounts.TryGetValue(rightVar.Identifier, out referenceCount) &&
(referenceCount == 1) && !rightVar.IsReference && rightVar.IsParameter)
return false;
}
return true;
}
示例3: Translate_PropertyCall
protected JSExpression Translate_PropertyCall(JSExpression thisExpression, JSMethod method, JSExpression[] arguments, bool @virtual, bool @static)
{
var propertyInfo = method.Method.DeclaringProperty;
if (propertyInfo == null)
return null;
if (propertyInfo.IsIgnored)
return new JSIgnoredMemberReference(true, propertyInfo, arguments);
// JS provides no way to override [], so keep it as a regular method call
if (propertyInfo.Member.IsIndexer())
return null;
var parms = method.Method.Metadata.GetAttributeParameters("JSIL.Meta.JSReplacement") ??
propertyInfo.Metadata.GetAttributeParameters("JSIL.Meta.JSReplacement");
if (parms != null) {
var argsDict = new Dictionary<string, JSExpression>();
argsDict["this"] = thisExpression;
argsDict["typeof(this)"] = new JSType(thisExpression.GetExpectedType(TypeSystem));
foreach (var kvp in method.Method.Parameters.Zip(arguments, (p, v) => new { p.Name, Value = v })) {
argsDict.Add(kvp.Name, kvp.Value);
}
return new JSVerbatimLiteral(method, (string)parms[0].Value, argsDict, propertyInfo.ReturnType);
}
var thisType = GetTypeDefinition(thisExpression.GetExpectedType(TypeSystem));
Func<JSExpression> generate = () => {
var actualThis = @static ? new JSType(method.Method.DeclaringType.Definition) : thisExpression;
if ((method.Reference.DeclaringType is GenericInstanceType) && !method.Reference.HasThis) {
actualThis = new JSType(method.Reference.DeclaringType);
}
if ((propertyInfo.Member.GetMethod != null) && (method.Method.Member.Name == propertyInfo.Member.GetMethod.Name)) {
return new JSPropertyAccess(
actualThis, new JSProperty(method.Reference, propertyInfo)
);
} else {
if (arguments.Length == 0)
throw new InvalidOperationException("Attempting to invoke a property setter with no arguments");
return new JSBinaryOperatorExpression(
JSOperator.Assignment,
new JSPropertyAccess(
actualThis, new JSProperty(method.Reference, propertyInfo)
),
arguments[0], propertyInfo.ReturnType
);
}
};
// Accesses to a base property should go through a regular method invocation, since
// javascript properties do not have a mechanism for base access
if (method.Method.Member.HasThis) {
if (!TypesAreEqual(method.Method.DeclaringType.Definition, thisType) && [email protected]) {
return null;
} else {
return generate();
}
}
return generate();
}
示例4: Translate_MethodReplacement
protected JSExpression Translate_MethodReplacement(
JSMethod method, JSExpression thisExpression,
JSExpression[] arguments, bool @virtual, bool @static, bool explicitThis
)
{
var methodInfo = method.Method;
var metadata = methodInfo.Metadata;
if (metadata != null) {
var parms = metadata.GetAttributeParameters("JSIL.Meta.JSReplacement");
if (parms != null) {
var argsDict = new Dictionary<string, JSExpression>();
argsDict["this"] = thisExpression;
argsDict["typeof(this)"] = new JSType(thisExpression.GetExpectedType(TypeSystem));
foreach (var kvp in methodInfo.Parameters.Zip(arguments, (p, v) => new { p.Name, Value = v })) {
argsDict.Add(kvp.Name, kvp.Value);
}
return new JSVerbatimLiteral(
method, (string)parms[0].Value, argsDict, method.Method.ReturnType
);
}
}
if (methodInfo.IsIgnored)
return new JSIgnoredMemberReference(true, methodInfo, new[] { thisExpression }.Concat(arguments).ToArray());
switch (method.Method.Member.FullName) {
case "System.Object JSIL.Builtins::Eval(System.String)":
return JSInvocationExpression.InvokeStatic(
JS.eval, arguments
);
case "System.Object JSIL.Verbatim::Expression(System.String)": {
var expression = arguments[0] as JSStringLiteral;
if (expression == null)
throw new InvalidOperationException("JSIL.Verbatim.Expression must recieve a string literal as an argument");
return new JSVerbatimLiteral(
method, expression.Value, null, null
);
}
case "System.Object JSIL.JSGlobal::get_Item(System.String)": {
var expression = arguments[0] as JSStringLiteral;
if (expression != null)
return new JSDotExpression(
JSIL.GlobalNamespace, new JSStringIdentifier(expression.Value, TypeSystem.Object)
);
else
return new JSIndexerExpression(
JSIL.GlobalNamespace, arguments[0], TypeSystem.Object
);
}
case "System.Object JSIL.JSLocal::get_Item(System.String)": {
var expression = arguments[0] as JSStringLiteral;
if (expression == null)
throw new InvalidOperationException("JSLocal must recieve a string literal as an index");
return new JSStringIdentifier(expression.Value, TypeSystem.Object);
}
case "System.Object JSIL.Builtins::get_This()":
return new JSIndirectVariable(Variables, "this", ThisMethodReference);
}
JSExpression result = Translate_PropertyCall(thisExpression, method, arguments, @virtual, @static);
if (result == null) {
if (@static)
result = JSInvocationExpression.InvokeStatic(method.Reference.DeclaringType, method, arguments);
else if (explicitThis)
result = JSInvocationExpression.InvokeBaseMethod(method.Reference.DeclaringType, method, thisExpression, arguments);
else
result = JSInvocationExpression.InvokeMethod(method.Reference.DeclaringType, method, thisExpression, arguments);
}
return result;
}
示例5: Translate_Conv
protected JSExpression Translate_Conv(JSExpression value, TypeReference expectedType)
{
var currentType = value.GetExpectedType(TypeSystem);
if (TypesAreEqual(expectedType, currentType))
return value;
int currentDepth, expectedDepth;
var currentDerefed = FullyDereferenceType(currentType, out currentDepth);
var expectedDerefed = FullyDereferenceType(expectedType, out expectedDepth);
// Handle assigning a value of type 'T&&' to a variable of type 'T&', etc.
// 'AreTypesAssignable' will return false, because the types are not equivalent, but no cast is necessary.
if (TypesAreEqual(expectedDerefed, currentDerefed)) {
if (currentDepth > expectedDepth) {
// If the current expression has more levels of reference than the target type, we must dereference
// the current expression one or more times to strip off the reference levels.
var result = value;
JSExpression dereferenced;
while (currentDepth > expectedDepth) {
bool ok = JSReferenceExpression.TryDereference(JSIL, result, out dereferenced);
if (!ok)
break;
currentDepth -= 1;
result = dereferenced;
}
return result;
} else {
return value;
}
}
if (IsDelegateType(expectedType) && IsDelegateType(currentType))
return value;
if (IsNumericOrEnum(currentType) && IsNumericOrEnum(expectedType)) {
if (IsIntegral(expectedType)) {
if (IsIntegral(currentType))
return value;
else
return JSInvocationExpression.InvokeStatic(JS.floor, new[] { value }, true);
} else {
return value;
}
} else if (!TypesAreAssignable(expectedType, currentType)) {
if (expectedType.FullName == "System.Boolean") {
if (IsIntegral(currentType)) {
// i != 0 sometimes becomes (bool)i, so we want to generate the explicit form
return new JSBinaryOperatorExpression(
JSOperator.NotEqual, value, JSLiteral.New(0), TypeSystem.Boolean
);
} else if (!currentType.IsValueType) {
// We need to detect any attempts to cast object references to boolean and not generate a cast
// for them, so that our logic in Translate_UnaryOp for detecting (x != null) will still work
return value;
}
}
// Never cast AnyType to another type since the implication is that the proxy author will ensure the correct
// type is returned.
if (currentType.FullName == "JSIL.Proxy.AnyType")
return value;
return JSIL.Cast(value, expectedType);
} else
return value;
}
示例6: Translate_Conv
protected JSExpression Translate_Conv(JSExpression value, TypeReference expectedType)
{
var currentType = value.GetExpectedType(TypeSystem);
try {
expectedType = TypeAnalysis.SubstituteTypeArgs(expectedType, ThisMethodReference);
} catch (OpenGenericTypeException) {
throw new AbortTranslation(String.Format("Cast value to open generic type '{0}'", expectedType));
}
if (IsDelegateType(expectedType) && IsDelegateType(currentType))
return value;
if (IsNumericOrEnum(currentType) && IsNumericOrEnum(expectedType)) {
if (IsIntegral(expectedType)) {
if (IsIntegral(currentType))
return value;
else
return JSInvocationExpression.InvokeStatic(JS.floor, new[] { value }, true);
} else {
return value;
}
} else if (!TypesAreAssignable(expectedType, currentType)) {
if (expectedType.FullName == "System.Boolean") {
if (IsIntegral(currentType)) {
// i != 0 sometimes becomes (bool)i, so we want to generate the explicit form
return new JSBinaryOperatorExpression(
JSOperator.NotEqual, value, JSLiteral.New(0), TypeSystem.Boolean
);
} else if (!currentType.IsValueType) {
// We need to detect any attempts to cast object references to boolean and not generate a cast
// for them, so that our logic in Translate_UnaryOp for detecting (x != null) will still work
return value;
}
}
return JSIL.Cast(value, expectedType);
} else
return value;
}
示例7: IsCopyNeeded
protected bool IsCopyNeeded(JSExpression value)
{
if ((value == null) || (value.IsNull))
return false;
while (value is JSReferenceExpression)
value = ((JSReferenceExpression)value).Referent;
var valueType = value.GetExpectedType(TypeSystem);
if (!IsStruct(valueType))
return false;
if (valueType.FullName.StartsWith("System.Nullable"))
return false;
if (
(value is JSLiteral) ||
(value is JSNewExpression) ||
(value is JSPassByReferenceExpression)
) {
return false;
}
var rightVar = value as JSVariable;
if (rightVar != null) {
int referenceCount;
if (ReferenceCounts.TryGetValue(rightVar.Identifier, out referenceCount) &&
(referenceCount == 1) && !rightVar.IsReference && rightVar.IsParameter)
return false;
}
return true;
}
示例8: NewReference
public JSNewExpression NewReference(JSExpression initialValue)
{
var resultType = new ByReferenceType(initialValue.GetExpectedType(TypeSystem));
return new JSNewExpression(
Dot("Variable", resultType),
null, initialValue
);
}
示例9: IsCopyNeeded
protected bool IsCopyNeeded(JSExpression value)
{
if ((value == null) || (value.IsNull))
return false;
while (value is JSReferenceExpression)
value = ((JSReferenceExpression)value).Referent;
var valueType = value.GetExpectedType(TypeSystem);
if (!IsStruct(valueType))
return false;
if (valueType.FullName.StartsWith("System.Nullable"))
return false;
if (
(value is JSLiteral) ||
(value is JSNewExpression) ||
(value is JSPassByReferenceExpression)
) {
return false;
}
var rightVar = value as JSVariable;
if (rightVar != null) {
int referenceCount;
if (
ReferenceCounts.TryGetValue(rightVar.Identifier, out referenceCount) &&
(referenceCount == 1) && !rightVar.IsReference && rightVar.IsParameter &&
!SecondPass.VariableAliases.ContainsKey(rightVar.Identifier)
) {
if (Tracing)
Debug.WriteLine(String.Format("Returning false from IsCopyNeeded for parameter {0} because reference count is 1 and it has no aliases", value));
return false;
}
}
return true;
}