2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
26
#if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
28
using System.Reflection;
30
using NUnit.Framework;
32
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
33
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
34
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
36
using Newtonsoft.Json.Serialization;
37
using Newtonsoft.Json.Utilities;
38
using Newtonsoft.Json.Tests.TestObjects;
39
using Newtonsoft.Json.Tests.Serialization;
41
namespace Newtonsoft.Json.Tests.Utilities
44
public class DynamicReflectionDelegateFactoryTests : TestFixtureBase
47
public void CreateGetWithBadObjectTarget()
49
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
52
Person p = new Person();
55
Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
62
public void CreateSetWithBadObjectTarget()
64
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
67
Person p = new Person();
68
Movie m = new Movie();
70
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
74
Assert.AreEqual(m.Name, "Hi");
81
public void CreateSetWithBadTarget()
83
ExceptionAssert.Throws<InvalidCastException>("Specified cast is not valid.",
86
object structTest = new StructTest();
88
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
90
setter(structTest, "Hi");
92
Assert.AreEqual("Hi", ((StructTest)structTest).StringProperty);
94
setter(new TimeSpan(), "Hi");
99
public void CreateSetWithBadObjectValue()
101
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'System.Version' to type 'System.String'.",
104
Movie m = new Movie();
106
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
108
setter(m, new Version("1.1.1.1"));
113
public void CreateStaticMethodCall()
115
MethodInfo castMethodInfo = typeof(JsonSerializerTest.DictionaryKey).GetMethod("op_Implicit", new[] { typeof(string) });
117
Assert.IsNotNull(castMethodInfo);
119
MethodCall<object, object> call = DynamicReflectionDelegateFactory.Instance.CreateMethodCall<object>(castMethodInfo);
121
object result = call(null, "First!");
122
Assert.IsNotNull(result);
124
JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey) result;
125
Assert.AreEqual("First!", key.Value);
129
//public void sdfsdf()
131
// string name = "MyAssembly";
132
// string filename = name + ".dll";
134
// AssemblyName s = new AssemblyName(name);
136
// AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(s, AssemblyBuilderAccess.RunAndSave);
137
// ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(filename, filename);
138
// TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Class, typeof(object));
139
// MethodBuilder methodBuilder = typeBuilder.DefineMethod("TestSet", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new[] { typeof(object), typeof(object) });
141
// DynamicReflectionDelegateFactory.GenerateCreateSetFieldIL(typeof(ClassWithGuid).GetField("GuidField"), methodBuilder.GetILGenerator());
142
// typeBuilder.CreateType();
143
// assemblyBuilder.Save(filename);
147
//public void sdfsdf1()
149
// string name = "MyAssembly1";
150
// string filename = name + ".dll";
152
// AssemblyName s = new AssemblyName(name);
154
// AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(s, AssemblyBuilderAccess.RunAndSave);
155
// ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(filename, filename);
156
// TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Class, typeof(object));
157
// MethodBuilder methodBuilder = typeBuilder.DefineMethod("TestSet", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new[] { typeof(object), typeof(object) });
159
// DynamicReflectionDelegateFactory.GenerateCreateSetPropertyIL(typeof(Car).GetProperty("Model"), methodBuilder.GetILGenerator());
160
// typeBuilder.CreateType();
161
// assemblyBuilder.Save(filename);
b'\\ No newline at end of file'