本文整理汇总了C#中TypeViewer类的典型用法代码示例。如果您正苦于以下问题:C# TypeViewer类的具体用法?C# TypeViewer怎么用?C# TypeViewer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeViewer类属于命名空间,在下文中一共展示了TypeViewer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryImplicitCoercion
public virtual Expression TryImplicitCoercion(Expression source, TypeNode targetType, TypeViewer typeViewer) {
ErrorHandler oldEH = this.ErrorHandler;
this.ErrorHandler = null;
Expression e = null;
try { e = this.ImplicitCoercion(source, targetType, typeViewer); }
finally {
this.ErrorHandler = oldEH;
};
return e;
}
示例2: GetCardinality
public virtual Cardinality GetCardinality(TypeNode collectionType, TypeViewer typeViewer) {
if (collectionType == null) return Cardinality.None;
TypeAlias ta = collectionType as TypeAlias;
if (ta != null) collectionType = ta.AliasedType;
if (collectionType is TupleType) {
return Cardinality.One;
}
else if (collectionType.Template == SystemTypes.GenericBoxed) {
return Cardinality.ZeroOrOne;
}
else if (collectionType.Template == SystemTypes.GenericNonNull) {
return Cardinality.One;
}
else if (collectionType.Template == SystemTypes.GenericNonEmptyIEnumerable) {
return Cardinality.OneOrMore;
}
else if (TypeViewer.GetTypeView(typeViewer, collectionType).IsAssignableTo(SystemTypes.INullable)) {
return Cardinality.ZeroOrOne;
}
else {
TypeUnion tu = collectionType as TypeUnion;
if (tu != null && tu.Types.Count > 0) {
Cardinality c = this.GetCardinality(tu.Types[0], typeViewer);
for( int i = 1, n = tu.Types.Count; i < n; i++ ) {
TypeNode tn = tu.Types[i];
if (tn == null) continue;
c = this.GetCardinalityOr(c, this.GetCardinality(tn, typeViewer));
}
return c;
}
TypeNode elementType = this.GetStreamElementType(collectionType, typeViewer);
if (elementType != collectionType) {
return Cardinality.ZeroOrMore;
}
else if (collectionType.IsValueType) {
return Cardinality.One;
}
else {
return Cardinality.None;
}
}
}
示例3: NotAccessible
public static bool NotAccessible(Member member, ref TypeNode qualifierType, int visibility, Module currentModule, TypeNode currentType, TypeViewer typeViewer) {
TypeNode type = member.DeclaringType;
return NotAccessible(member, type, ref qualifierType, visibility, currentModule, currentType, typeViewer);
}
示例4: GetCollectionElementType
public virtual TypeNode GetCollectionElementType(TypeNode t, TypeViewer typeViewer){
bool foundObject = false;
TypeAlias ta = t as TypeAlias;
while (ta != null){t = ta.AliasedType; ta = t as TypeAlias;}
if (t == null || t == SystemTypes.String || t is TupleType) return null;
// look for get_Item indexer
MemberList list = TypeViewer.GetTypeView(typeViewer, t).GetMembersNamed(StandardIds.getItem);
if (list != null) {
for( int i = 0, n = list.Count; i < n; i++ ) {
Method m = list[i] as Method;
if (m == null) continue;
if (m.ReturnType != SystemTypes.Object) return m.ReturnType;
foundObject = true;
}
}
// look for enumerable pattern
Method mge = TypeViewer.GetTypeView(typeViewer, t).GetMethod(StandardIds.GetEnumerator);
if (mge != null) {
Method mgc = TypeViewer.GetTypeView(typeViewer, mge.ReturnType).GetMethod(StandardIds.getCurrent);
if (mgc != null) {
if (mgc.ReturnType != SystemTypes.Object) return mgc.ReturnType;
foundObject = true;
}
}
InterfaceList ilist = TypeViewer.GetTypeView(typeViewer, t).Interfaces;
if (ilist != null) {
for( int i = 0, n = ilist.Count; i < n; i++ ) {
Interface iface = ilist[i];
if (iface == null) continue;
TypeNode tn = this.GetCollectionElementType(iface, typeViewer);
if (tn == null) continue;
if (tn != SystemTypes.Object) return tn;
foundObject = true;
}
}
if (foundObject) return SystemTypes.Object;
if (t.BaseType != null && t.BaseType != SystemTypes.Object) {
return this.GetCollectionElementType(t.BaseType, typeViewer);
}
return null;
}
示例5: GetMemberElementType
public virtual TypeNode GetMemberElementType(Member member, TypeViewer typeViewer) {
if (member == null) return null;
AttributeNode attr = MetadataHelper.GetCustomAttribute(member, SystemTypes.ElementTypeAttribute);
if (attr != null){
Literal litType = MetadataHelper.GetNamedAttributeValue(attr, StandardIds.ElementType);
if (litType != null) return litType.Value as TypeNode;
}
return this.GetStreamElementType(this.GetMemberType(member), typeViewer);
}
示例6: UnifiedType
public virtual TypeNode UnifiedType(Literal lit, TypeNode t, TypeViewer typeViewer){
if (lit == null || lit.Type == null || t == null){Debug.Assert(false); return SystemTypes.Object;}
t = this.Unwrap(t);
MemberList coercions = TypeViewer.GetTypeView(typeViewer, t).ImplicitCoercionMethods;
for (int i = 0, n = coercions == null ? 0 : coercions.Count; i < n; i++){
Method coercion = coercions[i] as Method;
if (coercion == null) continue;
TypeNode t2 = coercion.ReturnType;
if (t2 == t || t2 == null || !t2.IsPrimitive) continue;
if (this.ImplicitLiteralCoercionFromTo(lit, lit.Type, t2)) return t2;
}
return this.UnifiedType(lit.Type, t);
}
示例7: SupportedInterfaces
public virtual void SupportedInterfaces(TypeNode t, InterfaceList ifaceList, TypeViewer typeViewer){
if (ifaceList == null) return;
TypeNode unwrappedT = this.Unwrap(t);
Interface iface = unwrappedT as Interface;
if (iface != null){
// possibly not needed, but seems better to keep ifaceList as a set
int i = 0;
while (i < ifaceList.Count){
if (ifaceList[i] == iface)
break;
i++;
}
if (i == ifaceList.Count) // not found
ifaceList.Add(iface);
}else{
// nop
}
InterfaceList ifaces = TypeViewer.GetTypeView(typeViewer, unwrappedT).Interfaces;
for (int i = 0, n = ifaces == null ? 0 : ifaces.Count; i < n; i++){
this.SupportedInterfaces(ifaces[i],ifaceList,typeViewer);
}
return;
}
示例8: CoerceToTypeIntersection
public virtual Expression CoerceToTypeIntersection(Expression source, TypeNode sourceType, TypeIntersection targetType, bool explicitCoercion, TypeViewer typeViewer){
if (source == null || sourceType == null || targetType == null) return null;
if (!explicitCoercion){
TypeNodeList types = targetType.Types;
for (int i = 0, n = types == null ? 0 : types.Count; i < n; i++){
TypeNode t = types[i]; if (t == null) continue;
if (!TypeViewer.GetTypeView(typeViewer, sourceType).IsAssignableTo(t)) return null;
}
}
Method fromObject = TypeViewer.GetTypeView(typeViewer, targetType).GetMethod(StandardIds.FromObject, SystemTypes.Object);
Method getType = Runtime.GetType;
MethodCall fromObjectCall = new MethodCall(new MemberBinding(null, fromObject), new ExpressionList(source), NodeType.Call);
fromObjectCall.Type = targetType;
return fromObjectCall;
}
示例9: CoerceObjectToTypeUnion
public virtual Expression CoerceObjectToTypeUnion(Expression source, TypeUnion targetType, TypeViewer typeViewer){
Method fromObject = TypeViewer.GetTypeView(typeViewer, targetType).GetMethod(StandardIds.FromObject, SystemTypes.Object, SystemTypes.Type);
Method getType = Runtime.GetType;
ExpressionList arguments = new ExpressionList(2);
arguments.Add(source);
arguments.Add(new MethodCall(new MemberBinding(new Expression(NodeType.Dup), getType), null, NodeType.Call));
MethodCall fromObjectCall = new MethodCall(new MemberBinding(null, fromObject), arguments, NodeType.Call);
fromObjectCall.Type = targetType;
return fromObjectCall;
}
示例10: TupleCoercion
public virtual Expression TupleCoercion(Expression source, TypeNode sourceType, TypeNode targetType, bool explicitCoercion, TypeViewer typeViewer){
TupleType sTuple = sourceType as TupleType;
TupleType tTuple = targetType as TupleType;
if (sTuple == null){
if (!explicitCoercion) return null;
if (tTuple == null) return null;
MemberList tMems = tTuple.Members;
if (tMems == null || tMems.Count != 3) return null;
ConstructTuple consTuple = new ConstructTuple();
consTuple.Type = tTuple;
Field f = (Field)tMems[0].Clone();
consTuple.Fields = new FieldList(f);
if (f.Type is TypeAlias)
f.Initializer = this.ExplicitCoercion(source, f.Type, typeViewer);
else
f.Initializer = this.StandardExplicitCoercion(source, this.IsNonNullType(sourceType), sourceType, this.IsNonNullType(f.Type), f.Type, f.Type, typeViewer);
if (f.Initializer == null) return null;
return consTuple;
}
MemberList sMembers = sTuple.Members;
if (sMembers == null) return null;
int n = sMembers.Count;
if (tTuple == null){
if (n == 3){
TypeUnion tUnion = targetType as TypeUnion;
if (tUnion != null){
Method coercion = this.UserDefinedImplicitCoercionMethod(source, sourceType, targetType, true, typeViewer);
if (coercion != null)
return new MethodCall(new MemberBinding(null, coercion), new ExpressionList(this.ImplicitCoercion(source, coercion.Parameters[0].Type, typeViewer)),
NodeType.Call, coercion.ReturnType, source.SourceContext);
}
Field sField = sMembers[0] as Field;
if (sField == null || (!explicitCoercion && !this.ImplicitCoercionFromTo(sField.Type, targetType, typeViewer))) return null;
if (!sField.IsAnonymous && targetType == SystemTypes.Object)
return new BinaryExpression(source, new MemberBinding(null, sTuple), NodeType.Box, SystemTypes.Object);
ConstructTuple cTuple = source as ConstructTuple;
if (cTuple != null){
//TODO: give a warning
source = cTuple.Fields[0].Initializer;
}else{
MemberBinding mb = new MemberBinding(new UnaryExpression(source, NodeType.AddressOf), sField);
mb.Type = sField.Type;
source = mb;
}
if (explicitCoercion)
return this.ExplicitCoercion(source, targetType, typeViewer);
else
return this.ImplicitCoercion(source, targetType, typeViewer);
}
if (targetType == SystemTypes.Object)
return new BinaryExpression(source, new MemberBinding(null, sTuple), NodeType.Box, SystemTypes.Object);
return null;
}
MemberList tMembers = tTuple.Members;
if (sMembers == tMembers) return source;
if (tMembers == null) return null;
if (n != tMembers.Count) return null;
n-=2;
ConstructTuple consTup = source as ConstructTuple;
if (consTup != null){
FieldList consFields = consTup.Fields;
for (int i = 0; i < n; i++){
Field cField = consFields[i];
if (cField == null) continue;
Field tField = tMembers[i] as Field;
if (tField == null) return null;
if (explicitCoercion)
cField.Initializer = this.ExplicitCoercion(cField.Initializer, tField.Type, typeViewer);
else{
if (!tField.IsAnonymous && tField.Name != null &&
(cField.IsAnonymous || cField.Name == null || cField.Name.UniqueIdKey != tField.Name.UniqueIdKey)) return null;
cField.Initializer = this.ImplicitCoercion(cField.Initializer, tField.Type, typeViewer);
}
if (cField.Initializer == null) return null;
cField.Type = tField.Type;
}
consTup.Type = tTuple;
return consTup;
}
Local loc = new Local(sTuple);
CoerceTuple cTup = new CoerceTuple();
cTup.OriginalTuple = source;
cTup.Temp = loc;
cTup.Type = tTuple;
FieldList cFields = cTup.Fields = new FieldList(n);
for (int i = 0; i < n; i++){
Field sField = sMembers[i] as Field;
if (sField == null) return null;
Field tField = tMembers[i] as Field;
if (tField == null) return null;
Field cField = new Field();
cField.Type = tField.Type;
MemberBinding mb = new MemberBinding(loc, sField);
if (explicitCoercion)
cField.Initializer = this.ExplicitCoercion(mb, tField.Type, typeViewer);
else{
if (!tField.IsAnonymous && tField.Name != null &&
(sField.IsAnonymous || sField.Name == null || sField.Name.UniqueIdKey != tField.Name.UniqueIdKey)) return null;
cField.Initializer = this.ImplicitCoercion(mb, tField.Type, typeViewer);
}
//.........这里部分代码省略.........
示例11: ImplicitCoercionToIntersection
public virtual bool ImplicitCoercionToIntersection(TypeNode sourceType, TypeIntersection intersect, TypeViewer typeViewer){
if (intersect == null) return false;
TypeNodeList types = intersect.Types;
for (int i = 0, n = types == null ? 0 : types.Count; i < n; i++){
TypeNode t = types[i];
if (t == null) continue;
if (!this.ImplicitCoercionFromTo(sourceType, t, typeViewer)) return false;
}
return true;
}
示例12: StandardImplicitCoercion
protected virtual Expression StandardImplicitCoercion(Expression source, bool sourceIsNonNullType, TypeNode sourceType, bool targetIsNonNullType, TypeNode targetType, TypeNode originalTargetType, TypeViewer typeViewer){
if (Literal.IsNullLiteral(source)) {
if (this.IsNullableType(targetType)) {
Local temp = new Local(targetType);
StatementList statements = new StatementList();
BlockExpression result = new BlockExpression(new Block(statements));
statements.Add(new AssignmentStatement(new AddressDereference(new UnaryExpression(temp, NodeType.AddressOf), targetType), new Literal(null, CoreSystemTypes.Object)));
statements.Add(new ExpressionStatement(temp));
return result;
}
if (targetType.IsTemplateParameter && !targetType.IsReferenceType) {
// Check for reference constraint
this.HandleError(source, Error.TypeVarCantBeNull, targetType.Name.Name);
return new Local(targetType);
}
}
//Identity coercion
if (sourceType == targetType && (!targetIsNonNullType || (targetIsNonNullType == sourceIsNonNullType))) return source;
ITypeParameter stp = sourceType as ITypeParameter;
ITypeParameter ttp = targetType as ITypeParameter;
if (stp != null && ttp != null && stp.ParameterListIndex == ttp.ParameterListIndex && stp.DeclaringMember == ttp.DeclaringMember &&
(!targetIsNonNullType || (targetIsNonNullType == sourceIsNonNullType))) return source;
if (source is This && targetType != null && sourceType == targetType.Template && targetType.IsNotFullySpecialized)
//TODO: add check for sourceType.TemplateParameters == targetType.TemplateArguments
return source;
//Dereference source
Reference sr = sourceType as Reference;
if (sr != null){
sourceType = sr.ElementType;
Pointer pType = targetType as Pointer;
if (pType != null && this.StandardImplicitCoercionFromTo(null, sourceType, pType.ElementType, typeViewer))
return source;
else if (pType != null && pType.ElementType == SystemTypes.Void)
return source;
bool sourceIsThis = source is This;
source = new AddressDereference(source, sourceType, source.SourceContext);
source.Type = sourceType;
sourceIsNonNullType = this.IsNonNullType(sourceType);
sourceType = TypeNode.StripModifier(sourceType, SystemTypes.NonNullType);
//Special case for coercion of this in template class
if (sourceIsThis && targetType != null && sourceType == targetType.Template && targetType.IsNotFullySpecialized)
//TODO: add check for sourceType.TemplateParameters == targetType.TemplateArguments
return source;
}
//Identity coercion after dereference
if (sourceType == targetType && (!targetIsNonNullType || (targetIsNonNullType == sourceIsNonNullType))) return source;
//Special case for null literal
if (Literal.IsNullLiteral(source)){
if (targetIsNonNullType)
return ImplicitNonNullCoercion(this.ErrorHandler, source, originalTargetType);
if (targetType is ITypeParameter && this.useGenerics)
return new BinaryExpression(source, new Literal(targetType, SystemTypes.Type), NodeType.UnboxAny);
if (!targetType.IsValueType || targetType.Template == SystemTypes.GenericBoxed)
return new Literal(null, targetType, source.SourceContext);
if (this.IsNullableType(targetType))
return new Local(StandardIds.NewObj, targetType, source.SourceContext);
TypeAlias tAlias = targetType as TypeAlias;
if (tAlias != null){
if (tAlias.RequireExplicitCoercionFromUnderlyingType) return null;
source = this.ImplicitCoercion(source, tAlias.AliasedType, typeViewer);
if (source == null) return null;
Method coercion = this.UserDefinedImplicitCoercionMethod(source, tAlias.AliasedType, targetType, false, typeViewer);
if (coercion != null){
ExpressionList args = new ExpressionList(this.ImplicitCoercion(source, coercion.Parameters[0].Type, typeViewer));
return new MethodCall(new MemberBinding(null, coercion), args, NodeType.Call, coercion.ReturnType);
}
}else{
Method coercion = this.UserDefinedImplicitCoercionMethod(source, source.Type, targetType, true, typeViewer);
if (coercion != null){
ExpressionList args = new ExpressionList(this.ImplicitCoercion(source, coercion.Parameters[0].Type, typeViewer));
return new MethodCall(new MemberBinding(null, coercion), args, NodeType.Call, coercion.ReturnType);
}
}
this.HandleError(source, Error.CannotCoerceNullToValueType, this.GetTypeName(targetType));
return new Local(targetType);
}
//Special case for string literal
if (source.NodeType == NodeType.Literal && sourceType == SystemTypes.String &&
(targetType.Template == SystemTypes.GenericNonNull || targetType.Template == SystemTypes.GenericInvariant) &&
this.GetStreamElementType(targetType, typeViewer) == SystemTypes.String)
return this.ExplicitCoercion(source, targetType, typeViewer);
//Implicit numeric coercions + implicit enumeration coercions + implicit constant expression coercions
if (sourceType.IsPrimitive && sourceType != SystemTypes.String && (targetType.IsPrimitive || targetType == SystemTypes.Decimal || targetType is EnumNode)){
Expression primitiveCoercion = this.ImplicitPrimitiveCoercion(source, sourceType, targetType, typeViewer);
if (primitiveCoercion != null) return primitiveCoercion;
}
//Implicit coercion from string literal to numbers or eums
if (this.allowStringLiteralToOtherPrimitiveCoercion && sourceType == SystemTypes.String && (targetType.IsPrimitive || targetType == SystemTypes.Decimal || targetType is EnumNode)){
Expression primitiveCoercion = this.ImplicitPrimitiveCoercion(source, sourceType, targetType, typeViewer);
if (primitiveCoercion != null) return primitiveCoercion;
}
//Implicit reference coercions
if (TypeViewer.GetTypeView(typeViewer, sourceType).IsAssignableTo(targetType)){
if (targetIsNonNullType && !(sourceIsNonNullType) && !sourceType.IsValueType) {
//Handling for non null types
return ImplicitNonNullCoercion(this.ErrorHandler, source, originalTargetType);
}else if (sourceType.IsValueType && !targetType.IsValueType){
if (sourceType.NodeType == NodeType.TypeUnion){
Debug.Assert(targetType == SystemTypes.Object);
//.........这里部分代码省略.........
示例13: CoerceWithLiftedCoercion
public virtual Expression CoerceWithLiftedCoercion(Expression source, TypeNode sourceType, TypeNode targetType, Method coercion, bool explicitCoercion, TypeViewer typeViewer){
if (source == null || sourceType == null || targetType == null || coercion == null){Debug.Assert(false); return null;}
Block nullCase = new Block(new StatementList(1));
Block nonNullCase = new Block(new StatementList(1));
Block done = new Block();
Block coercionBlock = new Block(new StatementList(7));
Local copyOfSource = new Local(source.Type);
Local result = new Local(targetType);
//null case
StatementList statements = nullCase.Statements;
statements.Add(new AssignmentStatement(result, new Local(StandardIds.NewObj, targetType)));
//nonNull case
statements = nonNullCase.Statements;
Method getValue = TypeViewer.GetTypeView(typeViewer, sourceType).GetMethod(Identifier.For("get_Value"));
Expression getVal = new MethodCall(
new MemberBinding(new UnaryExpression(copyOfSource, NodeType.AddressOf, TypeViewer.GetTypeView(typeViewer, sourceType).GetReferenceType()), getValue), null, NodeType.Call, getValue.ReturnType);
if (explicitCoercion)
getVal = this.ExplicitCoercion(getVal, coercion.Parameters[0].Type, typeViewer);
else
getVal = this.ImplicitCoercion(getVal, coercion.Parameters[0].Type, typeViewer);
if (getVal == null) return null;
Expression nonNullVal = new MethodCall(new MemberBinding(null, coercion), new ExpressionList(getVal), NodeType.Call, coercion.ReturnType);
statements.Add(new AssignmentStatement(result, this.ImplicitCoercion(nonNullVal, targetType, typeViewer)));
//coercion block
statements = coercionBlock.Statements;
statements.Add(new AssignmentStatement(copyOfSource, source));
Method hasValue = TypeViewer.GetTypeView(typeViewer, sourceType).GetMethod(StandardIds.getHasValue);
if (hasValue == null){Debug.Assert(false); return null;}
Expression ifNonNull = new MethodCall(
new MemberBinding(new UnaryExpression(copyOfSource, NodeType.AddressOf, TypeViewer.GetTypeView(typeViewer, sourceType).GetReferenceType()), hasValue), null, NodeType.Call);
statements.Add(new Branch(ifNonNull, nonNullCase));
statements.Add(nullCase);
statements.Add(new Branch(null, done));
statements.Add(nonNullCase);
statements.Add(done);
statements.Add(new ExpressionStatement(result));
return new BlockExpression(coercionBlock, targetType);
}
示例14: ImplicitCoercion
public virtual Expression ImplicitCoercion(Expression source, TypeNode targetType, TypeViewer typeViewer){
TypeNode originalTargetType = targetType;
if (targetType == null || targetType.Name == Looker.NotFound) return source;
if (source == null) return null;
//HS D
if (source is Hole)
{
source.Type = targetType;
return source;
}
//HS D
if (source is LambdaHole)
{
if (targetType == SystemTypes.Boolean)
source = new LambdaHole(source, new Literal(0), NodeType.Ge, source.SourceContext);
source.Type = targetType;
return source;
}
Literal sourceLit = source as Literal;
if (sourceLit != null && sourceLit.Value is TypeNode){
this.HandleError(source, Error.TypeInVariableContext, this.GetTypeName((TypeNode)sourceLit.Value), "class", "variable");
return null;
}
//Ignore parentheses
if (source.NodeType == NodeType.Parentheses){
UnaryExpression uex = (UnaryExpression)source;
uex.Operand = this.ImplicitCoercion(uex.Operand, targetType, typeViewer);
if (uex.Operand == null) return null;
uex.Type = uex.Operand.Type;
return uex;
}
bool targetIsNonNullType = this.IsNonNullType(targetType);
targetType = TypeNode.StripModifier(targetType, SystemTypes.NonNullType);
targetType = TypeNode.StripModifier(targetType, SystemTypes.NullableType);
//TODO: handle SkipCheck and EnforceCheck
//Special case for closure expressions
if (source.NodeType == NodeType.AnonymousNestedFunction)
return this.CoerceAnonymousNestedFunction((AnonymousNestedFunction)source, targetType, false, typeViewer);
TypeNode sourceType = source.Type;
if (sourceType == null) sourceType = SystemTypes.Object;
bool sourceIsNonNullType = this.IsNonNullType(source.Type);
sourceType = TypeNode.StripModifier(sourceType, SystemTypes.NonNullType);
sourceType = TypeNode.StripModifier(sourceType, SystemTypes.NullableType);
if (sourceType == SystemTypes.String && !sourceIsNonNullType && source is Literal)
sourceIsNonNullType = ((Literal)source).Value != null;
if (this.currentParameter != null && targetType is Reference){
UnaryExpression uex = source as UnaryExpression;
if (uex != null){
if (sourceIsNonNullType && !targetIsNonNullType){
string ttypeName = this.GetTypeName(targetType);
string stypeName = this.GetTypeName(source.Type);
this.HandleError(source, Error.NoImplicitCoercion, stypeName, ttypeName);
return null;
}
if (!sourceIsNonNullType && targetIsNonNullType){
string ttypeName = this.GetTypeName(targetType);
string stypeName = this.GetTypeName(source.Type);
this.HandleError(source, Error.NoImplicitCoercion, stypeName, ttypeName);
return null;
}
if (uex.NodeType == NodeType.OutAddress){
if ((this.currentParameter.Flags & ParameterFlags.Out) == 0){
this.currentParameter.Flags |= ParameterFlags.Out;
string stypeName = this.GetTypeName(sourceType);
this.currentParameter.Flags &= ~ParameterFlags.Out;
this.HandleError(source, Error.NoImplicitCoercion, stypeName, this.GetTypeName(targetType));
return null;
}
}else if (uex.NodeType == NodeType.RefAddress){
if ((this.currentParameter.Flags & ParameterFlags.Out) != 0){
this.currentParameter.Flags &= ~ParameterFlags.Out;
string stypeName = this.GetTypeName(sourceType);
this.currentParameter.Flags |= ParameterFlags.Out;
this.HandleError(source, Error.NoImplicitCoercion, stypeName, this.GetTypeName(targetType));
return null;
}
}
}
}
Expression result = this.StandardImplicitCoercion(source, sourceIsNonNullType, sourceType, targetIsNonNullType, targetType, originalTargetType, typeViewer);
if (result != null) return result;
Method coercion = this.UserDefinedImplicitCoercionMethod(source, sourceType, targetType, true, typeViewer);
if (coercion != null){
if (this.IsNullableType(targetType) && this.IsNullableType(sourceType) && !this.IsNullableType(coercion.Parameters[0].Type))
return this.CoerceWithLiftedCoercion(source, sourceType, targetType, coercion, false, typeViewer);
ExpressionList args = new ExpressionList(1);
args.Add(this.ImplicitCoercion(source, coercion.Parameters[0].Type, typeViewer));
return this.ImplicitCoercion(new MethodCall(new MemberBinding(null, coercion), args, NodeType.Call, coercion.ReturnType, source.SourceContext), targetType, typeViewer);
}
if (sourceType == SystemTypes.Type && source is Literal)
this.HandleError(source, Error.TypeInVariableContext, this.GetTypeName((TypeNode)((Literal)source).Value), "class", "variable");
else if (this.IsNullableType(sourceType) && this.IsNullableType(targetType) && this.ImplicitCoercionFromTo(this.RemoveNullableWrapper(sourceType), this.RemoveNullableWrapper(targetType))) {
TypeNode usType = this.RemoveNullableWrapper(sourceType);
TypeNode utType = this.RemoveNullableWrapper(targetType);
Local tempSrc = new Local(sourceType);
Local tempTar = new Local(targetType);
StatementList statements = new StatementList();
BlockExpression result1 = new BlockExpression(new Block(statements));
statements.Add(new AssignmentStatement(tempSrc, source));
//.........这里部分代码省略.........
示例15: UserDefinedImplicitCoercionMethod
public virtual Method UserDefinedImplicitCoercionMethod(Expression source, TypeNode sourceType, TypeNode targetType, bool tryStandardCoercions, TypeViewer typeViewer){
Reference rtype = sourceType as Reference;
if (rtype != null) sourceType = rtype.ElementType;
if (tryStandardCoercions && this.IsNullableType(sourceType) && this.IsNullableType(targetType)) {
sourceType = sourceType.TemplateArguments[0];
targetType = targetType.TemplateArguments[0];
}
//First do efficient searches for a method that implicitly coerces directly between source and target type
//If the source type knows how to convert to the target type, give it preference
Method coercion = TypeViewer.GetTypeView(typeViewer, sourceType).GetImplicitCoercionToMethod(targetType);
if (coercion != null) return coercion;
//If the target type knows how to convert from the source type, that is dandy too
coercion = TypeViewer.GetTypeView(typeViewer, targetType).GetImplicitCoercionFromMethod(sourceType);
if (coercion != null) return coercion;
//Perhaps the base type can convert to the target type, or the target type can convert from the base type
if (sourceType.BaseType != null && sourceType != SystemTypes.Object){
coercion = this.UserDefinedImplicitCoercionMethod(source, sourceType.BaseType, targetType, tryStandardCoercions, typeViewer);
if (coercion != null) return coercion;
}
if (!tryStandardCoercions) return null;
//Now resort to desperate measures
//See if the source type has a conversion that results in a type that can be converted to the target type via a standard coercion
MemberList coercions = TypeViewer.GetTypeView(typeViewer, sourceType).ImplicitCoercionMethods;
for (int i = 0, n = coercions == null ? 0 : coercions.Count; i < n; i++){
coercion = coercions[i] as Method;
if (coercion == null) continue;
if (coercion.ReturnType == sourceType) continue;
if (this.StandardImplicitCoercionFromTo(source, coercion.ReturnType, targetType, typeViewer)) return coercion;
}
//See if the target type has a conversion that can convert the source after a standard coercion has been applied
coercions = TypeViewer.GetTypeView(typeViewer, targetType).ImplicitCoercionMethods;
for (int i = 0, n = coercions == null ? 0 : coercions.Count; i < n; i++){
coercion = coercions[i] as Method;
if (coercion == null) continue;
if (coercion.ReturnType != targetType) continue;
ParameterList pars = coercion.Parameters;
if (pars == null || pars.Count != 1) continue;
Parameter par = pars[0];
if (par.Type == null) continue;
if (this.StandardImplicitCoercionFromTo(source, sourceType, par.Type, typeViewer)) return coercion;
}
return null;
}