private static object CallCtorDelegate(XamlTypeInvoker type)
 {
     object uninitializedObject = FormatterServices.GetUninitializedObject(type._xamlType.UnderlyingType);
     InvokeDelegate(type._constructorDelegate, uninitializedObject);
     return uninitializedObject;
 }
Exemplo n.º 2
0
		public void DefaultValues ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (object), sctx));
			Assert.IsNull (i.SetMarkupExtensionHandler, "#1");
			Assert.IsNull (i.SetTypeConverterHandler, "#2");
		}
Exemplo n.º 3
0
		public void SetHandleMarkupExtensionInvalid3 ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (TestClassMarkupExtension3), sctx));
			Assert.IsNull (i.SetMarkupExtensionHandler, "#1");
		}
Exemplo n.º 4
0
		public void CreateInstanceList_ArgumentMismatch ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
			i.CreateInstance (new object [] {"foo"});
		}
Exemplo n.º 5
0
		public void CreateInstanceList ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
			i.CreateInstance (new object [0]);
		}
Exemplo n.º 6
0
		public void AddToCollectionNoUnderlyingType ()
		{
			var i = new XamlTypeInvoker (new XamlType ("urn:foo", "FooType", null, sctx));
			i.AddToCollection (new List<int> (), 5); // ... passes.
		}
Exemplo n.º 7
0
		public void CreateInstanceArray ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (int []), sctx));
			i.CreateInstance (new object [0]); // no default constructor.
		}
Exemplo n.º 8
0
		public void AddToCollectionList ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
			var l = new List<int> ();
			i.AddToCollection (l, 5);
			i.AddToCollection (l, 3);
			i.AddToCollection (l, -12);
			Assert.AreEqual (3, l.Count, "#1");
			Assert.AreEqual (-12, l [2], "#2");
		}
Exemplo n.º 9
0
		public void AddToCollectionTypeMismatch ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
			var l = new List<int> ();
			i.AddToCollection (l, "5");
		}
Exemplo n.º 10
0
		public void AddToCollectionList_ObjectTypeMismatch4 ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<Uri>), sctx));
			i.AddToCollection (new List<TimeSpan> (), TimeSpan.Zero); // it is allowed too.
		}
Exemplo n.º 11
0
		public void AddToCollectionList_NonCollectionType ()
		{
			// so, the source collection type is not checked at all.
			var i = new XamlTypeInvoker (new XamlType (typeof (Uri), sctx));
			i.AddToCollection (new List<TimeSpan> (), TimeSpan.Zero); // it is allowed too.
		}
Exemplo n.º 12
0
		public void AddToCollectionList_ObjectTypeMismatch3 ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<object>), sctx));
			i.AddToCollection (new List<int> (), 5); // it is allowed too.
		}
Exemplo n.º 13
0
		public void AddToCollectionList_ObjectTypeMismatch ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
			try {
				i.AddToCollection (new ArrayExtension (), 5);
				Assert.Fail ("not supported operation.");
			} catch (NotSupportedException) {
			} catch (TargetException) {
				// .NET throws this, but the difference should not really matter.
			}
		}
Exemplo n.º 14
0
		public void AddToCollectionArrayInstance ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (int []), sctx));
			var ax = new ArrayExtension ();
			i.AddToCollection (ax, 5);
		}
 public static object CreateInstance(XamlTypeInvoker type)
 {
     if (!EnsureConstructorDelegate(type))
     {
         return null;
     }
     return CallCtorDelegate(type);
 }
Exemplo n.º 16
0
		public void CreateInstanceNoUnderlyingType ()
		{
			var i = new XamlTypeInvoker (new XamlType ("urn:foo", "FooType", null, sctx));
			i.CreateInstance (new object [0]); // unkown type is not supported
		}
 private static bool EnsureConstructorDelegate(XamlTypeInvoker type)
 {
     if (type._constructorDelegate != null)
     {
         return true;
     }
     if (!type.IsPublic)
     {
         return false;
     }
     if (s_securityFailureWithCtorDelegate == ThreeValuedBool.NotSet)
     {
         s_securityFailureWithCtorDelegate = !AppDomain.CurrentDomain.PermissionSet.IsUnrestricted() ? ThreeValuedBool.True : ThreeValuedBool.False;
     }
     if (s_securityFailureWithCtorDelegate == ThreeValuedBool.True)
     {
         return false;
     }
     try
     {
         Type underlyingSystemType = type._xamlType.UnderlyingType.UnderlyingSystemType;
         ConstructorInfo constructor = underlyingSystemType.GetConstructor(Type.EmptyTypes);
         if (constructor == null)
         {
             throw new MissingMethodException(System.Xaml.SR.Get("NoDefaultConstructor", new object[] { underlyingSystemType.FullName }));
         }
         if ((constructor.IsSecurityCritical && !constructor.IsSecuritySafeCritical) || (((constructor.Attributes & MethodAttributes.HasSecurity) == MethodAttributes.HasSecurity) || ((underlyingSystemType.Attributes & TypeAttributes.HasSecurity) == TypeAttributes.HasSecurity)))
         {
             type._isPublic = ThreeValuedBool.False;
             return false;
         }
         IntPtr functionPointer = constructor.MethodHandle.GetFunctionPointer();
         object[] parameters = new object[2];
         parameters[1] = functionPointer;
         type._constructorDelegate = (Action<object>) s_actionCtor.Invoke(parameters);
         return true;
     }
     catch (SecurityException)
     {
         s_securityFailureWithCtorDelegate = ThreeValuedBool.True;
         return false;
     }
 }
Exemplo n.º 18
0
		public void SetHandleTypeConverter ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (TestClassTypeConverter4), sctx));
			Assert.IsNotNull (i.SetTypeConverterHandler, "#1");
		}