Наследование: System.Windows.Markup.MarkupExtension
Пример #1
0
		public void ProvideValueConst ()
		{
			var x = new StaticExtension ();
			x.MemberType = typeof (XamlLanguage);
			x.Member = "Xaml2006Namespace";
			Assert.AreEqual (XamlLanguage.Xaml2006Namespace, x.ProvideValue (null), "#1");
		}
Пример #2
0
		public void ProvideValueStaticProperty ()
		{
			var x = new StaticExtension ();
			x.MemberType = typeof (XamlLanguage);
			x.Member = "Array";
			Assert.AreEqual (XamlLanguage.Array, x.ProvideValue (null), "#1");
		}
Пример #3
0
		public void ProvideValueInstanceProperty ()
		{
			var x = new StaticExtension ();
			x.MemberType = typeof (StaticExtension);
			x.Member = "MemberType"; // instance property is out of scope.
			x.ProvideValue (null);
		}
Пример #4
0
		public void ProvideValueTest_nonIXamlTypeResolver ()
		{
			StaticExtension se = new StaticExtension ("Test.Field");

			ProviderPoker p = new ProviderPoker (new object());

			Assert.IsNull (se.ProvideValue (p));
		}
Пример #5
0
		public void CtorTest ()
		{
			StaticExtension s = new StaticExtension ();
			Assert.IsNull (s.Member);

			s = new StaticExtension ("hi");
			Assert.AreEqual ("hi", s.Member);
		}
Пример #6
0
		public void ProvideValueTest_nullService ()
		{
			StaticExtension se = new StaticExtension ("Test.Field");

			ProviderPoker p = new ProviderPoker (null);

			Assert.IsNull (se.ProvideValue (p));
		}
Пример #7
0
		public void ProvideValueWithoutType ()
		{
			var x = new StaticExtension ();
			// it fails because it cannot be resolved to a static member.
			// This possibly mean, there might be a member that 
			// could be resolved only with the name, without type.
			x.Member = "Foo";
			x.ProvideValue (null);
		}
Пример #8
0
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if (destinationType == typeof(InstanceDescriptor))
     {
         StaticExtension staticExtension = value as StaticExtension;
         if (staticExtension == null)
         {
             throw new ArgumentException(SR.Get(SRID.MustBeOfType, "value", "StaticExtension"));
         }
         return(new InstanceDescriptor(typeof(StaticExtension).GetConstructor(new Type[] { typeof(string) }),
                                       new object[] { staticExtension.Member }));
     }
     return(base.ConvertTo(context, culture, value, destinationType));
 }
Пример #9
0
		public void ProvideValueTest_IXamlTypeResolver_PrivateStaticProperty ()
		{
			StaticExtension ne = new StaticExtension ("Test.Property");

			ProviderPoker2 p = new ProviderPoker2 (typeof (PrivateStaticPropertyTest));

			Assert.AreEqual (5, ne.ProvideValue (p));
		}
Пример #10
0
		public void ProvideValueTest_IXamlTypeResolver_Enum ()
		{
			StaticExtension ne = new StaticExtension ("Test.Foo");

			ProviderPoker2 p = new ProviderPoker2 (typeof (EnumTest));

			Assert.AreEqual (EnumTest.Foo, ne.ProvideValue (p));
		}
Пример #11
0
		public void ProvideValueTest_IXamlTypeResolver_StaticField ()
		{
			StaticExtension ne = new StaticExtension ("Test.Field");

			ProviderPoker2 p = new ProviderPoker2 (typeof (StaticFieldTest));

			Assert.AreEqual (5, ne.ProvideValue (p));
		}
Пример #12
0
		public void ProvideValueTest_IXamlTypeResolver_MissingField ()
		{
			StaticExtension ne = new StaticExtension ("Test.Field2");

			ProviderPoker2 p = new ProviderPoker2 (typeof (StaticFieldTest));

			ne.ProvideValue (p);
		}
Пример #13
0
        // The end of the constructor parameter section has been reached.  Create an
        // instance of the object after finding the appropriate constructor and converting 
        // all of the objects held on the stack. 
        internal virtual void ReadConstructorParametersEndRecord()
        { 
            Type   elementType = ParentContext.ExpectedType;
            short  positiveElementTypeId = (short)-ParentContext.ExpectedTypeId;

            object param = null; 
            ArrayList paramList = null;
            int paramCount; 
            object instance = null; 
            bool foundInstance = false;
 
            if( TraceMarkup.IsEnabled )
            {
                TraceMarkup.Trace( TraceEventType.Start,
                                 TraceMarkup.CreateMarkupExtension, 
                                 elementType );
            } 
 
            if (CurrentContext.CheckFlag(ReaderFlags.SingletonConstructorParam))
            { 
                param = CurrentContext.ObjectData;
                paramCount = 1;

                // Fast code path for [static/dynamic] resource extensions & 
                // Type/Static/TemplateBinding extensions
                switch (positiveElementTypeId) 
                { 
                    case (short)KnownElements.TypeExtension:
 
                        // Note that this assumes that TypeExtension has a
                        // constructor with one param of type Type or String.
                        Type t = param as Type;
                        if (t != null) 
                        {
                            instance = new TypeExtension(t); 
                        } 
                        else
                        { 
                            Debug.Assert(param is String);
                            instance = new TypeExtension((String)param);
                        }
 
                        foundInstance = true;
                        break; 
 
                    case (short)KnownElements.StaticResourceExtension:
 
                        // Note that this assumes that StaticResourceExtension has a
                        // constructor with one param of type object.
                        instance = new StaticResourceExtension(param);
                        foundInstance = true; 
                        break;
 
                    case (short)KnownElements.DynamicResourceExtension: 

                        // Note that this assumes that DynamicResourceExtension has a 
                        // constructor with one param of type object.
                        instance = new DynamicResourceExtension(param);
                        foundInstance = true;
                        break; 

                    case (short)KnownElements.StaticExtension: 
 
                        // Note that this assumes that StaticExtension has a default
                        // constructor and one public property of type string and one 
                        // internal property of type object for optimized member info.
                        instance = new StaticExtension((string)param);
                        foundInstance = true;
                        break; 

                    case (short)KnownElements.TemplateBindingExtension: 
 
                        // Note that this assumes that TemplateBindingExtension has a
                        // constructor with one param of type DependencyProperty. If a 
                        // string is passed in due to there being other attributes like
                        // converter being set, then that needs to be converted now first.
                        DependencyProperty dp = param as DependencyProperty;
                        if (dp == null) 
                        {
                            string paramString = param as string; 
                            Type ownerType = ParserContext.TargetType; 
                            Debug.Assert(paramString != null);
 
                            dp = XamlTypeMapper.ParsePropertyName(ParserContext,
                                                                  paramString.Trim(),
                                                                  ref ownerType);
 
                            if (dp == null)
                            { 
                                ThrowException(SRID.ParserNoDPOnOwner, paramString, ownerType.FullName); 
                            }
                        } 

                        instance = new TemplateBindingExtension(dp);
                        foundInstance = true;
                        break; 
                }
            } 
            else 
            {
                paramList = (ArrayList)CurrentContext.ObjectData; 
                paramCount = paramList.Count;
            }

            if (!foundInstance) 
            {
                // Find the constructor based on the number of parameters stored in paramList 
                XamlTypeMapper.ConstructorData data = XamlTypeMapper.GetConstructors(elementType); 
                ConstructorInfo[] infos = data.Constructors;
                for (int i=0; i<infos.Length; i++) 
                {
                    ConstructorInfo info = infos[i];
                    ParameterInfo[] paramInfos = data.GetParameters(i);
                    if (paramInfos.Length == paramCount) 
                    {
                        object[] paramArray = new object[paramInfos.Length]; 
 
                        if (paramCount == 1)
                        { 
                            Debug.Assert(param != null && paramList == null, "Must have a single param");
                            ProcessConstructorParameter(paramInfos[0], param, ref paramArray[0]);

                            // Fast code path for other markupextensions 
                            if (positiveElementTypeId == (short)KnownElements.RelativeSource)
                            { 
                                // Note that this assumes that RelativeSource has a 
                                // constructor with one param of type RelativeSourceMode.
                                instance = new System.Windows.Data.RelativeSource((System.Windows.Data.RelativeSourceMode)paramArray[0]); 
                                foundInstance = true;
                            }
                        }
                        else 
                        {
                            Debug.Assert(param == null && paramList != null, "Must have a paramList"); 
 
                            // Check each type and attempt to convert the paramList using
                            // the type converter associated with each parameter type. 
                            for (int j=0; j<paramInfos.Length; j++)
                            {
                                ProcessConstructorParameter(paramInfos[j], paramList[j], ref paramArray[j]);
                            } 
                        }
 
                        if (!foundInstance) 
                        {
                            // If we make it to here we have a list of converted parameters, so 
                            // invoke the constructor with that list.
#if !STRESS
                            try
                            { 
#endif
                                instance = info.Invoke(paramArray); 
                                foundInstance = true; 
#if !STRESS
                            } 
                            catch (Exception e)
                            {
                                if (CriticalExceptions.IsCriticalException(e) || e is XamlParseException)
                                { 
                                    throw;
                                } 
 
                                TargetInvocationException tie = e as TargetInvocationException;
                                if( tie != null ) 
                                {
                                    e = tie.InnerException;
                                }
 
                                ThrowExceptionWithLine(SR.Get(SRID.ParserFailedToCreateFromConstructor, info.DeclaringType.Name),  e);
 
                            } 
#endif
                        } 
                    }
                }
            }
 
            if (foundInstance)
            { 
                ParentContext.ObjectData = instance; 
                ParentContext.ExpectedType = null;
                PopContext(); 
            }
            else
            {
                // If we get to here, then no matching constructor was found, so complain 
                ThrowException(SRID.ParserBadConstructorParams, elementType.Name, paramCount.ToString(CultureInfo.CurrentCulture));
            } 
 
            if( TraceMarkup.IsEnabled )
            { 
                TraceMarkup.Trace( TraceEventType.Stop,
                                 TraceMarkup.CreateMarkupExtension,
                                 elementType,
                                 instance ); 
            }
 
        } 
Пример #14
0
		public void ProvideValuePrivateConst ()
		{
			var x = new StaticExtension ();
			x.MemberType = GetType ();
			x.Member = "FooBar"; // private const could not be resolved.
			Assert.AreEqual ("foobar", x.ProvideValue (null), "#1");
		}
Пример #15
0
		public void ProvideValueWithoutMember ()
		{
			var x = new StaticExtension ();
			x.MemberType = typeof (int);
			x.ProvideValue (null);
		}
Пример #16
0
        private object GetStaticExtensionValue(short memberId) 
        {
            object valueObject = null; 
 
            if (memberId < 0)
            { 
                short keyId = (short)-memberId;
                // if keyId is more than the range it is the actual resource, else it is the key.
                if (keyId > (short)SystemResourceKeyID.InternalSystemThemeStylesEnd)
                { 
                    keyId -= (short)SystemResourceKeyID.InternalSystemThemeStylesEnd;
                    valueObject = SystemResourceKey.GetResource(keyId); 
                } 
                else
                { 
                    valueObject = SystemResourceKey.GetResourceKey(keyId);
                }
            }
            else 
            {
                BamlAttributeInfoRecord attribInfo = MapTable.GetAttributeInfoFromId(memberId); 
                if (attribInfo != null) 
                {
                    StaticExtension se = new StaticExtension(); 
                    se.MemberType = MapTable.GetTypeFromId(attribInfo.OwnerTypeId);
                    se.Member = attribInfo.Name;
                    valueObject = se.ProvideValue(null);
                } 
            }
 
            return valueObject; 
        }
Пример #17
0
		public void ProvideValueEvent ()
		{
			var x = new StaticExtension ();
			x.MemberType = GetType ();
			x.Member = "FooEvent"; // private const could not be resolved.
			Assert.IsNotNull (x.ProvideValue (null), "#1");
		}