Esempio n. 1
0
        public override Type OutputType(out CompiledFragment v)
        {
            v = this;

            Type type = OfType();

            // Map to functionality:
            CompiledClass Class     = null;
            bool          isDynamic = Types.IsDynamic(type);

            // (Constant) binding flags:
            BindingFlags flags = BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;

            if (Name == "length" && type.IsGenericType && !isDynamic)
            {
                // Does length actually exist as a field/ property?

                Field = type.GetField(Name, flags);

                if (Field == null)
                {
                    Property = type.GetProperty(Name, flags);

                    if (Property == null)
                    {
                        // Assume we meant count instead:
                        Name = "Count";
                    }
                }
            }

            if (isDynamic)
            {
                Class = Method.Script.GetClass(type);
            }
            else if (!Method.Script.AllowUse(type))
            {
                Error("Unable to access properties of type " + type + " as it has not been made accessible.");
            }

            if (isDynamic)
            {
                Field = Class.GetField(Name);
            }
            else
            {
                Field = type.GetField(Name, flags);
            }

            if (Field != null)
            {
                if (IsStatic && !Field.IsStatic)
                {
                    Error("Property " + Name + " is not static. You must use an object reference to access it.");
                }

                return(Field.FieldType);
            }

            if (isDynamic)
            {
                Property = Class.GetProperty(Name);
            }
            else
            {
                Property = type.GetProperty(Name, flags);
            }

            if (Property != null)
            {
                if (IsStatic)
                {
                    MethodInfo staticTest = Property.GetGetMethod();
                    if (staticTest == null)
                    {
                        staticTest = Property.GetSetMethod();
                    }
                    if (!staticTest.IsStatic)
                    {
                        Error("Property " + Name + " is not static. You must use an object reference to access it.");
                    }
                }
                return(Property.PropertyType);
            }

            if (isDynamic)
            {
                MethodReturnType = Class.MethodReturnType(Name);
            }
            else
            {
                MethodReturnType = Types.MethodReturnType(type, Name);
            }

            if (MethodReturnType != null)
            {
                if (Types.IsVoid(MethodReturnType))
                {
                    MethodReturnType = typeof(Void);
                }
                return(DynamicMethodCompiler.TypeFor(MethodReturnType));
            }

            if (Of.GetType() == typeof(ThisOperation))
            {
                // This was the first property - it can potentially be a static type name too.
                Type staticType = Method.Script.GetType(Name);
                if (staticType != null)
                {
                    // It's a static type! Generate a new type operation to replace this one and return the type.
                    v = new TypeOperation(Method, staticType);
                    return(v.OutputType(out v));
                }
            }

            if (Name == "this")
            {
                // This is handled here as it allows variables called "This". Use case: PowerUI.
                v = new ThisOperation(Method);
                return(v.OutputType(out v));
            }

            // Does it support indexing? If so, Do Parent["property"] instead.

            MethodOperation mOp = null;

            if (Input0 != null)
            {
                // This is a set. Input0 is the object we're setting.

                Type setType = Input0.OutputType(out Input0);

                // Get the set method:
                MethodInfo mInfo;

                if (isDynamic)
                {
                    mInfo = Class.FindMethodOverload("set_Item", new Type[] { typeof(string), setType });
                }
                else
                {
                    // Grab all the methods of the type:
                    MethodInfo[] allMethods = type.GetMethods();
                    mInfo = Types.GetOverload(allMethods, "set_Item", new Type[] { typeof(string), setType });
                }

                if (mInfo == null)
                {
                    // Try finding the extension method:
                    mInfo = FindExtensionMethod(type, "set");

                    if (mInfo == null)
                    {
                        // It doesn't exist!
                        // Error("Property '"+ToString()+"' is not a property or extension of "+type.ToString()+".");

                        // Create as a global:
                        Field = Method.Script.MainClass.DefineField(Name, true, setType);

                        return(setType);
                    }

                    // Extension property or method.
                    // -> We only know which based on MethodOperation later calling GetOverload.
                    //    Or OutputSet/ OutputIL being called.

                    return(mInfo.ReturnType);
                }

                // It exists - create the method operation now.
                mOp          = new MethodOperation(Method, mInfo, new CompiledFragment(Name), Input0);
                v            = mOp;
                mOp.CalledOn = Of;
                return(setType);
            }
            else
            {
                // Get.

                // Get the get method:
                MethodInfo mInfo;

                if (isDynamic)
                {
                    mInfo = Class.FindMethodOverload("get_Item", new Type[] { typeof(string) });
                }
                else
                {
                    // Grab all the methods of the type:
                    MethodInfo[] allMethods = type.GetMethods();
                    mInfo = Types.GetOverload(allMethods, "get_Item", new Type[] { typeof(string) });
                }


                if (mInfo == null)
                {
                    // Try finding the extension method:
                    mInfo = FindExtensionMethod(type, "get");

                    if (mInfo == null)
                    {
                        // It doesn't exist!
                        // Error("Property '"+ToString()+"' is not a property or extension of "+type.ToString()+".");

                        // Create as a global:
                        Field = Method.Script.MainClass.DefineField(Name, true, typeof(object));

                        return(typeof(object));
                    }

                    // Extension property or method.
                    // -> We only know which based on MethodOperation later calling GetOverload.
                    //    Or OutputSet/ OutputIL being called.

                    return(mInfo.ReturnType);
                }

                // It exists - create the method operation now:
                mOp          = new MethodOperation(Method, mInfo, new CompiledFragment(Name));
                v            = mOp;
                mOp.CalledOn = Of;
                return(mInfo.ReturnType);
            }
        }
		public override Type OutputType(out CompiledFragment v){
			v=this;
			
			Type type=OfType();
			
			// Map to functionality:
			CompiledClass Class=null;
			bool isDynamic=Types.IsDynamic(type);
			
			if(!isDynamic){
				if(!Method.Script.AllowUse(type)){
					Error("Unable to access properties of type "+type+" as it has not been made accessible.");
				}
			}else{
				Class=Method.Script.GetClass(type);
			}
			
			if(isDynamic){
				Field=Class.GetField(Name);
			}else{
				Field=type.GetField(Name,BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
			}
			
			if(Field!=null){
				if(IsStatic&&!Field.IsStatic){
					Error("Property "+Name+" is not static. You must use an object reference to access it.");
				}
				
				return Field.FieldType;
			}
			
			if(isDynamic){
				Property=Class.GetProperty(Name);
			}else{
				Property=type.GetProperty(Name,BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
			}
			
			if(Property!=null){
				if(IsStatic){
					MethodInfo staticTest=Property.GetGetMethod();
					if(staticTest==null){
						staticTest=Property.GetSetMethod();
					}
					if(!staticTest.IsStatic){
						Error("Property "+Name+" is not static. You must use an object reference to access it.");
					}
				}
				return Property.PropertyType;
			}
			
			if(isDynamic){
				MethodReturnType=Class.MethodReturnType(Name);
			}else{
				MethodReturnType=Types.MethodReturnType(type,Name);
			}
			
			if(MethodReturnType!=null){
				if(Types.IsVoid(MethodReturnType)){
					MethodReturnType=typeof(Void);
				}
				return DynamicMethodCompiler.TypeFor(MethodReturnType);
			}
			
			if(Of.GetType()==typeof(ThisOperation)){
				// This was the first property - it can potentially be a static type name too.
				Type staticType=Method.Script.GetType(Name);
				if(staticType!=null){
					// It's a static type! Generate a new type operation to replace this one and return the type.
					v=new TypeOperation(Method,staticType);
					return v.OutputType(out v);
				}
			}
			
			if(Name=="this"){
				// This is handled here as it allows variables called "This". Use case: PowerUI.
				v=new ThisOperation(Method);
				return v.OutputType(out v);
			}
			
			// Does it support indexing? If so, Do Parent["property"] instead.
			
			MethodOperation mOp=null;
			
			if(Input0!=null){
				// This is a set. Input0 is the object we're setting.
				
				Type setType=Input0.OutputType(out Input0);
				
				// Get the set method:
				MethodInfo mInfo;
				
				if(isDynamic){
					mInfo=Class.FindMethodOverload("set_Item",new Type[]{typeof(string),setType});
				}else{
					// Grab all the methods of the type:
					MethodInfo[] allMethods=type.GetMethods();
					mInfo=Types.GetOverload(allMethods,"set_Item",new Type[]{typeof(string),setType});
				}
				
				if(mInfo==null){
					// It doesn't exist!
					Error("Property '"+ToString()+"' is not a property of "+type.ToString()+".");
				}
				
				// It exists - create the method operation now.
				mOp=new MethodOperation(Method,mInfo,new CompiledFragment(Name),Input0);
				v=mOp;
				mOp.CalledOn=Of;
				return setType;
			}else{
				// Get.
				
				// Get the get method:
				MethodInfo mInfo;
				
				if(isDynamic){
					mInfo=Class.FindMethodOverload("get_Item",new Type[]{typeof(string)});
				}else{
					// Grab all the methods of the type:
					MethodInfo[] allMethods=type.GetMethods();
					mInfo=Types.GetOverload(allMethods,"get_Item",new Type[]{typeof(string)});
				}
				
				
				if(mInfo==null){
					// It doesn't exist!
					Error("'"+ToString()+"' is not a property of "+type.ToString()+".");
				}
				
				// It exists - create the method operation now:
				mOp=new MethodOperation(Method,mInfo,new CompiledFragment(Name));
				v=mOp;
				mOp.CalledOn=Of;
				return mInfo.ReturnType;
			}
			
		}