private static object CallCtorDelegate(XamlTypeInvoker type)
 {
     object uninitializedObject = FormatterServices.GetUninitializedObject(type._xamlType.UnderlyingType);
     InvokeDelegate(type._constructorDelegate, uninitializedObject);
     return uninitializedObject;
 }
Beispiel #2
0
		public void DefaultValues ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (object), sctx));
			Assert.IsNull (i.SetMarkupExtensionHandler, "#1");
			Assert.IsNull (i.SetTypeConverterHandler, "#2");
		}
Beispiel #3
0
		public void SetHandleMarkupExtensionInvalid3 ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (TestClassMarkupExtension3), sctx));
			Assert.IsNull (i.SetMarkupExtensionHandler, "#1");
		}
Beispiel #4
0
		public void CreateInstanceList_ArgumentMismatch ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
			i.CreateInstance (new object [] {"foo"});
		}
Beispiel #5
0
		public void CreateInstanceList ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
			i.CreateInstance (new object [0]);
		}
Beispiel #6
0
		public void AddToCollectionNoUnderlyingType ()
		{
			var i = new XamlTypeInvoker (new XamlType ("urn:foo", "FooType", null, sctx));
			i.AddToCollection (new List<int> (), 5); // ... passes.
		}
Beispiel #7
0
		public void CreateInstanceArray ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (int []), sctx));
			i.CreateInstance (new object [0]); // no default constructor.
		}
Beispiel #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");
		}
Beispiel #9
0
		public void AddToCollectionTypeMismatch ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (List<int>), sctx));
			var l = new List<int> ();
			i.AddToCollection (l, "5");
		}
Beispiel #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.
		}
Beispiel #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.
		}
Beispiel #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.
		}
Beispiel #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.
			}
		}
Beispiel #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);
 }
Beispiel #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;
     }
 }
Beispiel #18
0
		public void SetHandleTypeConverter ()
		{
			var i = new XamlTypeInvoker (new XamlType (typeof (TestClassTypeConverter4), sctx));
			Assert.IsNotNull (i.SetTypeConverterHandler, "#1");
		}