├── Benchmarks ├── Benchmarks.csproj ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── EmitMapper.All.sln ├── EmitMapper.SL.sln ├── EmitMapper.sln ├── EmitMapper ├── AST │ ├── CompilationContext.cs │ ├── Helpers │ │ ├── AstBuildHelper.cs │ │ └── CompilationHelper.cs │ ├── ILCompilationException.cs │ ├── Interfaces │ │ ├── IAstAddr.cs │ │ ├── IAstNode.cs │ │ ├── IAstRef.cs │ │ ├── IAstRefOrAddr.cs │ │ ├── IAstRefOrValue.cs │ │ ├── IAstStackItem.cs │ │ └── IAstValue.cs │ └── Nodes │ │ ├── AstBox.cs │ │ ├── AstCallMethod.cs │ │ ├── AstCallMethodVoid.cs │ │ ├── AstCastClass.cs │ │ ├── AstComplexNode.cs │ │ ├── AstConstantInt32.cs │ │ ├── AstConstantNull.cs │ │ ├── AstConstantString.cs │ │ ├── AstExceptionHandlingBlock.cs │ │ ├── AstExprEquals.cs │ │ ├── AstExprIsNull.cs │ │ ├── AstExprNot.cs │ │ ├── AstIf.cs │ │ ├── AstIfNull.cs │ │ ├── AstIfTernar.cs │ │ ├── AstIndirectRead.cs │ │ ├── AstInitializeLocalVariable.cs │ │ ├── AstNewNullable.cs │ │ ├── AstNewObject.cs │ │ ├── AstReadArgument.cs │ │ ├── AstReadArrayItem.cs │ │ ├── AstReadField.cs │ │ ├── AstReadLocal.cs │ │ ├── AstReadProperty.cs │ │ ├── AstReadThis.cs │ │ ├── AstReturn.cs │ │ ├── AstReturnVoid.cs │ │ ├── AstThrow.cs │ │ ├── AstTypeof.cs │ │ ├── AstUnbox.cs │ │ ├── AstValueToAddr.cs │ │ ├── AstWriteField.cs │ │ ├── AstWriteLocal.cs │ │ └── AstWriteProperty.cs ├── AbstractTypeBuilder.cs ├── Conversion │ ├── ArraysConverter.cs │ ├── EMConvert.cs │ ├── NativeConvert.cs │ ├── NullableConverter.cs │ └── StaticConvertersManager.cs ├── DynamicAssemblyManager.cs ├── EmitBuilders │ ├── BuilderUtils.cs │ ├── CreateTargetInstanceBuilder.cs │ ├── MappingBuilder.cs │ └── MappingOperationsProcessor.cs ├── EmitInvoker │ ├── Delegates │ │ ├── DelegateInvoker.cs │ │ ├── DelegateInvokerAction_0.cs │ │ ├── DelegateInvokerAction_1.cs │ │ ├── DelegateInvokerAction_2.cs │ │ ├── DelegateInvokerAction_3.cs │ │ ├── DelegateInvokerBase.cs │ │ ├── DelegateInvokerFunc_0.cs │ │ ├── DelegateInvokerFunc_1.cs │ │ ├── DelegateInvokerFunc_2.cs │ │ └── DelegateInvokerFunc_3.cs │ └── Methods │ │ ├── MethodInvoker.cs │ │ ├── MethodInvokerAction_0.cs │ │ ├── MethodInvokerAction_1.cs │ │ ├── MethodInvokerAction_2.cs │ │ ├── MethodInvokerAction_3.cs │ │ ├── MethodInvokerBase.cs │ │ ├── MethodInvokerFunc_0.cs │ │ ├── MethodInvokerFunc_1.cs │ │ ├── MethodInvokerFunc_2.cs │ │ └── MethodInvokerFunc_3.cs ├── EmitMapper.csproj ├── EmitMapperException.cs ├── Mappers │ ├── CustomMapperImpl.cs │ ├── MapperForClassImpl.cs │ ├── MapperForCollectionImpl.cs │ ├── MapperPrimitiveImpl.cs │ └── ObjectsMapperBaseImpl.cs ├── MappingConfiguration │ ├── CustomConverterDescriptor.cs │ ├── CustomMapConfig.cs │ ├── DefaultCustomConverterProvider.cs │ ├── DefaultMapConfig.cs │ ├── ICustomConverter.cs │ ├── ICustomConverterProvider.cs │ ├── IMappingConfigurator.cs │ ├── MapConfigBase.cs │ ├── MapConfigBaseImpl.cs │ ├── MappingOperations │ │ ├── DestSrcReadOperation.cs │ │ ├── DestWriteOperation.cs │ │ ├── Interfaces │ │ │ ├── IComplexOperation.cs │ │ │ ├── IDestOperation.cs │ │ │ ├── IDestReadOperation.cs │ │ │ ├── IDestWriteOperation.cs │ │ │ ├── IMappingOperation.cs │ │ │ ├── IReadWriteOperation.cs │ │ │ ├── IRootMappingOperation.cs │ │ │ ├── ISrcOperation.cs │ │ │ ├── ISrcReadOperation.cs │ │ │ ├── ISrcWriteOperation.cs │ │ │ └── OperationdDiagram.cd │ │ ├── MappingOperationDelegates.cs │ │ ├── OperationsBlock.cs │ │ ├── ReadWriteComplex.cs │ │ ├── ReadWriteSimple.cs │ │ ├── RootMappingOperation.cs │ │ └── SrcReadOperation.cs │ ├── MemberDescriptor.cs │ └── TypeList.cs ├── ObjectMapperManager.cs ├── ObjectsMapper.cs ├── Properties │ └── AssemblyInfo.cs └── Utils │ ├── MiscUtils.cs │ ├── ReflectionUtils.cs │ └── ThreadSaveCache.cs ├── EmitMapperTests.NUnit ├── Context.cs ├── CustomAssert.cs ├── CustomMapping.cs ├── EmitInvokerTest.cs ├── EmitMapperTests.NUnit.csproj ├── EmitMapperTests.NUnit.csproj.user ├── EnumTests.cs ├── Flattering.cs ├── GeneralTests.cs ├── IgnoreByAttributes.cs ├── NullableTypes.cs ├── Properties │ └── AssemblyInfo.cs ├── TestUtils.cs ├── TypeConversion.cs ├── ValueTypes.cs ├── testProject.VisualState.xml └── testProject.nunit ├── Samples ├── EMConfigurations │ ├── CaseIgnoreMapping.cs │ ├── EMConfigurations.csproj │ ├── FlatteringConfig.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── EmitMapper.Mvc.Net │ ├── EmitMapper.Mvc.Net.csproj │ ├── FormCollectionMapConfig.cs │ └── Properties │ │ └── AssemblyInfo.cs └── LightDataAccess │ ├── CmdParams.cs │ ├── CommandBuilder.Insert.cs │ ├── CommandBuilder.Update.cs │ ├── ConvertUtils.cs │ ├── DBTools.cs │ ├── DataReaderToObjectMapper.cs │ ├── DbSettings.cs │ ├── LightDataAccess.csproj │ ├── MappingConfigs │ └── AddDbCommandsMappingConfig.cs │ ├── ObjectsChangeTracker.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── SelectConstraints.cs │ └── ThreadConnection.cs ├── SamplesTests ├── App.config ├── CustormTests.cs ├── Flattering.cs ├── Properties │ └── AssemblyInfo.cs ├── SamplesTests.csproj ├── TestMappingToDataRow.cs └── TestObjectsTracker.cs ├── Silverlight ├── EmitMapper.SL │ ├── EmitMapper.SL.csproj │ └── Properties │ │ └── AssemblyInfo.cs ├── EmitMapperTests.SL │ ├── EmitMapperTests.SL.csproj │ └── Properties │ │ └── AssemblyInfo.cs ├── NUnit.SL │ └── framework.sl │ │ ├── AssemblyInfo.cs │ │ ├── Assert.cs │ │ ├── AssertionException.cs │ │ ├── AssertionHelper.cs │ │ ├── Assume.cs │ │ ├── CategoryAttribute.cs │ │ ├── CollectionAssert.cs │ │ ├── CommonAssemblyInfo.cs │ │ ├── Constraints │ │ ├── AttributeConstraints.cs │ │ ├── BasicConstraints.cs │ │ ├── BinaryOperations.cs │ │ ├── CollectionConstraints.cs │ │ ├── ComparisonAdapter.cs │ │ ├── ComparisonConstraints.cs │ │ ├── Constraint.cs │ │ ├── ConstraintBuilder.cs │ │ ├── ConstraintExpression.cs │ │ ├── ConstraintExpressionBase.cs │ │ ├── ConstraintFactory.cs │ │ ├── ConstraintOperators.cs │ │ ├── ContainsConstraint.cs │ │ ├── DelayedConstraint.cs │ │ ├── DirectoryConstraints.cs │ │ ├── EmptyConstraint.cs │ │ ├── EqualConstraint.cs │ │ ├── EqualityAdapter.cs │ │ ├── FloatingPointNumerics.cs │ │ ├── IResolveConstraint.cs │ │ ├── MessageWriter.cs │ │ ├── MsgUtils.cs │ │ ├── NUnitComparer.cs │ │ ├── NUnitEqualityComparer.cs │ │ ├── Numerics.cs │ │ ├── PathConstraints.cs │ │ ├── PredicateConstraint.cs │ │ ├── PrefixConstraints.cs │ │ ├── PropertyConstraint.cs │ │ ├── RangeConstraint.cs │ │ ├── ResolvableConstraintExpression.cs │ │ ├── SameAsConstraint.cs │ │ ├── SerializableConstraints.cs │ │ ├── StringConstraints.cs │ │ ├── ThrowsConstraint.cs │ │ ├── Tolerance.cs │ │ └── TypeConstraints.cs │ │ ├── Contains.cs │ │ ├── DatapointAttributes.cs │ │ ├── DescriptionAttribute.cs │ │ ├── DirectoryAssert.cs │ │ ├── ExpectedExceptionAttribute.cs │ │ ├── ExplicitAttribute.cs │ │ ├── FileAssert.cs │ │ ├── GlobalSettings.cs │ │ ├── Has.cs │ │ ├── IExpectException.cs │ │ ├── ITestCaseData.cs │ │ ├── IgnoreAttribute.cs │ │ ├── IgnoreException.cs │ │ ├── IncludeExcludeAttributes.cs │ │ ├── InconclusiveException.cs │ │ ├── Is.cs │ │ ├── Iz.cs │ │ ├── JoinTypeAttributes.cs │ │ ├── List.cs │ │ ├── ListMapper.cs │ │ ├── MaxTimeAttribute.cs │ │ ├── PropertyAttribute.cs │ │ ├── RandomAttribute.cs │ │ ├── Randomizer.cs │ │ ├── RangeAttribute.cs │ │ ├── RepeatAttribute.cs │ │ ├── RequiredAddinAttribute.cs │ │ ├── SetCultureAttribute.cs │ │ ├── SetUICultureAttribute.cs │ │ ├── SetUpAttribute.cs │ │ ├── SetUpFixtureAttribute.cs │ │ ├── SpecialValue.cs │ │ ├── StringAssert.cs │ │ ├── SuccessException.cs │ │ ├── SuiteAttribute.cs │ │ ├── SyntaxElements.txt │ │ ├── TearDownAttribute.cs │ │ ├── TestAttribute.cs │ │ ├── TestCaseAttribute.cs │ │ ├── TestCaseData.cs │ │ ├── TestCaseSourceAttribute.cs │ │ ├── TestFixtureAttribute.cs │ │ ├── TestFixtureSetUpAttribute.cs │ │ ├── TestFixtureTearDownAttribute.cs │ │ ├── Text.cs │ │ ├── TextMessageWriter.cs │ │ ├── TheoryAttribute.cs │ │ ├── ThreadingAttributes.cs │ │ ├── Throws.cs │ │ ├── ValueSourceAttribute.cs │ │ ├── ValuesAttribute.cs │ │ └── nunit.framework.sl.dll.csproj ├── SL.Specific │ ├── ApplicationException.cs │ ├── ArrayList.cs │ ├── Comparer.cs │ ├── Convert.cs │ ├── Environment.cs │ ├── HashSet.cs │ ├── ICloneable.cs │ ├── IFormatterConverter.cs │ ├── ISerializable.cs │ ├── SerializationInfo.cs │ └── SerializationInfoEnumerator.cs ├── TestsLauncher.SL.Web │ ├── Default.aspx │ ├── Default.aspx.cs │ ├── Default.aspx.designer.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Silverlight.js │ ├── TestsLauncher.SL.Web.csproj │ ├── TestsLauncher.SLTestPage.aspx │ ├── TestsLauncher.SLTestPage.html │ └── Web.config └── TestsLauncher.SL │ ├── App.xaml │ ├── App.xaml.cs │ ├── MainPage.xaml │ ├── MainPage.xaml.cs │ ├── Properties │ ├── AppManifest.xml │ └── AssemblyInfo.cs │ └── TestsLauncher.SL.csproj ├── libs ├── BenchmarkLibs │ ├── AutoMapper.dll │ ├── BLToolkit.3.dll │ └── LinFu.DynamicProxy.dll └── NUnit │ ├── lib │ ├── Failure.png │ ├── Ignored.png │ ├── Inconclusive.png │ ├── Skipped.png │ ├── Success.png │ ├── nunit-gui-runner.dll │ ├── nunit.core.dll │ ├── nunit.core.interfaces.dll │ ├── nunit.fixtures.dll │ ├── nunit.uiexception.dll │ ├── nunit.uikit.dll │ └── nunit.util.dll │ ├── nunit.exe │ ├── nunit.exe.config │ └── nunit.framework.dll └── readme.md /Benchmarks/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Benchmarks")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Benchmarks")] 13 | [assembly: AssemblyCopyright("Copyright © 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("4b75914c-db23-42e0-9480-ba33a6257e56")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /EmitMapper/AST/Helpers/CompilationHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection.Emit; 4 | using System.Reflection; 5 | using EmitMapper.AST.Interfaces; 6 | 7 | namespace EmitMapper.AST.Helpers 8 | { 9 | class CompilationHelper 10 | { 11 | public static void EmitCall( 12 | CompilationContext context, 13 | IAstRefOrAddr invocationObject, 14 | MethodInfo methodInfo, 15 | List arguments) 16 | { 17 | if (arguments == null) 18 | { 19 | arguments = new List(); 20 | } 21 | if (invocationObject != null) 22 | { 23 | invocationObject.Compile(context); 24 | } 25 | 26 | ParameterInfo[] args = methodInfo.GetParameters(); 27 | if (args.Length != arguments.Count) 28 | { 29 | throw new Exception("Invalid method parameters count"); 30 | } 31 | 32 | for (int i = 0; i < args.Length; ++i) 33 | { 34 | arguments[i].Compile(context); 35 | PrepareValueOnStack(context, args[i].ParameterType, arguments[i].itemType); 36 | } 37 | if (methodInfo.IsVirtual) 38 | { 39 | context.EmitCall(OpCodes.Callvirt, methodInfo); 40 | } 41 | else 42 | { 43 | context.EmitCall(OpCodes.Call, methodInfo); 44 | } 45 | } 46 | 47 | public static void PrepareValueOnStack(CompilationContext context, Type desiredType, Type typeOnStack) 48 | { 49 | if (typeOnStack.IsValueType && !desiredType.IsValueType) 50 | { 51 | context.Emit(OpCodes.Box, typeOnStack); 52 | } 53 | else if (!typeOnStack.IsValueType && desiredType.IsValueType) 54 | { 55 | context.Emit(OpCodes.Unbox_Any, desiredType); 56 | } 57 | else if (desiredType != typeOnStack) 58 | { 59 | context.Emit(OpCodes.Castclass, desiredType); 60 | } 61 | } 62 | 63 | public static void CheckIsRef(Type type) 64 | { 65 | if (type.IsValueType) 66 | { 67 | throw new ILCompilationException("A reference type was expected, but it was: " + type); 68 | } 69 | } 70 | 71 | public static void CheckIsValue(Type type) 72 | { 73 | if (!type.IsValueType) 74 | { 75 | throw new ILCompilationException("A value type was expected, but it was: " + type); 76 | } 77 | } 78 | 79 | } 80 | } -------------------------------------------------------------------------------- /EmitMapper/AST/ILCompilationException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace EmitMapper.AST 4 | { 5 | class ILCompilationException:Exception 6 | { 7 | public ILCompilationException(string message) 8 | : base(message) 9 | { 10 | } 11 | 12 | public ILCompilationException(string message, params object[] p) 13 | : base(String.Format(message, p)) 14 | { 15 | } 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Interfaces/IAstAddr.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace EmitMapper.AST.Interfaces 6 | { 7 | interface IAstAddr : IAstRefOrAddr 8 | { 9 | } 10 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Interfaces/IAstNode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Reflection.Emit; 5 | 6 | namespace EmitMapper.AST.Interfaces 7 | { 8 | interface IAstNode 9 | { 10 | void Compile(CompilationContext context); 11 | } 12 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Interfaces/IAstRef.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace EmitMapper.AST.Interfaces 6 | { 7 | interface IAstRef : IAstRefOrValue, IAstRefOrAddr 8 | { 9 | } 10 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Interfaces/IAstRefOrAddr.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace EmitMapper.AST.Interfaces 6 | { 7 | interface IAstRefOrAddr : IAstStackItem 8 | { 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Interfaces/IAstRefOrValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace EmitMapper.AST.Interfaces 6 | { 7 | interface IAstRefOrValue: IAstStackItem 8 | { 9 | } 10 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Interfaces/IAstStackItem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace EmitMapper.AST.Interfaces 4 | { 5 | interface IAstStackItem: IAstNode 6 | { 7 | Type itemType { get; } 8 | } 9 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Interfaces/IAstValue.cs: -------------------------------------------------------------------------------- 1 | namespace EmitMapper.AST.Interfaces 2 | { 3 | interface IAstValue : IAstRefOrValue 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstBox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Reflection.Emit; 5 | using EmitMapper.AST.Interfaces; 6 | 7 | namespace EmitMapper.AST.Nodes 8 | { 9 | class AstBox : IAstRef 10 | { 11 | public IAstRefOrValue value; 12 | 13 | #region IAstReturnValueNode Members 14 | 15 | public Type itemType 16 | { 17 | get 18 | { 19 | return value.itemType; 20 | } 21 | } 22 | 23 | #endregion 24 | 25 | #region IAstNode Members 26 | 27 | public void Compile(CompilationContext context) 28 | { 29 | value.Compile(context); 30 | 31 | if (value.itemType.IsValueType) 32 | { 33 | context.Emit(OpCodes.Box, itemType); 34 | } 35 | } 36 | 37 | #endregion 38 | } 39 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstCallMethod.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using EmitMapper.AST.Helpers; 5 | using EmitMapper.AST.Interfaces; 6 | 7 | 8 | namespace EmitMapper.AST.Nodes 9 | { 10 | class AstCallMethod: IAstRefOrValue 11 | { 12 | public MethodInfo methodInfo; 13 | public IAstRefOrAddr invocationObject; 14 | public List arguments; 15 | 16 | public AstCallMethod( 17 | MethodInfo methodInfo, 18 | IAstRefOrAddr invocationObject, 19 | List arguments) 20 | { 21 | if (methodInfo == null) 22 | { 23 | throw new InvalidOperationException("methodInfo is null"); 24 | } 25 | this.methodInfo = methodInfo; 26 | this.invocationObject = invocationObject; 27 | this.arguments = arguments; 28 | } 29 | 30 | public Type itemType 31 | { 32 | get 33 | { 34 | return methodInfo.ReturnType; 35 | } 36 | } 37 | 38 | public virtual void Compile(CompilationContext context) 39 | { 40 | CompilationHelper.EmitCall(context, invocationObject, methodInfo, arguments); 41 | } 42 | } 43 | 44 | class AstCallMethodRef : AstCallMethod, IAstRef 45 | { 46 | public AstCallMethodRef(MethodInfo methodInfo, IAstRefOrAddr invocationObject, List arguments) 47 | : base(methodInfo, invocationObject, arguments) 48 | { 49 | } 50 | 51 | override public void Compile(CompilationContext context) 52 | { 53 | CompilationHelper.CheckIsRef(itemType); 54 | base.Compile(context); 55 | } 56 | } 57 | 58 | class AstCallMethodValue : AstCallMethod, IAstValue 59 | { 60 | public AstCallMethodValue(MethodInfo methodInfo, IAstRefOrAddr invocationObject, List arguments) 61 | : base(methodInfo, invocationObject, arguments) 62 | { 63 | } 64 | override public void Compile(CompilationContext context) 65 | { 66 | CompilationHelper.CheckIsValue(itemType); 67 | base.Compile(context); 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstCallMethodVoid.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection.Emit; 3 | using System.Reflection; 4 | using EmitMapper.AST.Interfaces; 5 | 6 | namespace EmitMapper.AST.Nodes 7 | { 8 | class AstCallMethodVoid : IAstNode 9 | { 10 | protected MethodInfo methodInfo; 11 | protected IAstRefOrAddr invocationObject; 12 | protected List arguments; 13 | 14 | public AstCallMethodVoid( 15 | MethodInfo methodInfo, 16 | IAstRefOrAddr invocationObject, 17 | List arguments) 18 | { 19 | this.methodInfo = methodInfo; 20 | this.invocationObject = invocationObject; 21 | this.arguments = arguments; 22 | } 23 | 24 | public void Compile(CompilationContext context) 25 | { 26 | new AstCallMethod(methodInfo, invocationObject, arguments).Compile(context); 27 | 28 | if (methodInfo.ReturnType != typeof(void)) 29 | { 30 | context.Emit(OpCodes.Pop); 31 | } 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstCastClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.AST.Interfaces; 6 | using System.Reflection.Emit; 7 | using EmitMapper.AST.Helpers; 8 | 9 | namespace EmitMapper.AST.Nodes 10 | { 11 | class AstCastclass : IAstRefOrValue 12 | { 13 | protected IAstRefOrValue _value; 14 | protected Type _targetType; 15 | 16 | public AstCastclass(IAstRefOrValue value, Type targetType) 17 | { 18 | _value = value; 19 | _targetType = targetType; 20 | } 21 | 22 | #region IAstStackItem Members 23 | 24 | public Type itemType 25 | { 26 | get { return _targetType; } 27 | } 28 | 29 | #endregion 30 | 31 | #region IAstNode Members 32 | 33 | public virtual void Compile(CompilationContext context) 34 | { 35 | 36 | if (_value.itemType != _targetType) 37 | { 38 | if (!_value.itemType.IsValueType && !_targetType.IsValueType) 39 | { 40 | _value.Compile(context); 41 | context.Emit(OpCodes.Castclass, _targetType); 42 | return; 43 | } 44 | else if (_targetType.IsValueType && !_value.itemType.IsValueType) 45 | { 46 | new AstUnbox() { refObj = (IAstRef)_value, unboxedType = _targetType }.Compile(context); 47 | return; 48 | } 49 | 50 | throw new EmitMapperException(); 51 | } 52 | else 53 | { 54 | _value.Compile(context); 55 | } 56 | } 57 | 58 | #endregion 59 | } 60 | 61 | class AstCastclassRef : AstCastclass, IAstRef 62 | { 63 | public AstCastclassRef(IAstRefOrValue value, Type targetType): base(value, targetType) 64 | { 65 | } 66 | 67 | override public void Compile(CompilationContext context) 68 | { 69 | CompilationHelper.CheckIsRef(itemType); 70 | base.Compile(context); 71 | } 72 | } 73 | 74 | class AstCastclassValue : AstCastclass, IAstValue 75 | { 76 | public AstCastclassValue(IAstRefOrValue value, Type targetType): base(value, targetType) 77 | { 78 | } 79 | 80 | override public void Compile(CompilationContext context) 81 | { 82 | CompilationHelper.CheckIsValue(itemType); 83 | base.Compile(context); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstComplexNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using EmitMapper.AST.Interfaces; 3 | 4 | namespace EmitMapper.AST.Nodes 5 | { 6 | class AstComplexNode: IAstNode 7 | { 8 | public List nodes = new List(); 9 | 10 | public void Compile(CompilationContext context) 11 | { 12 | foreach (IAstNode node in nodes) 13 | { 14 | if (node != null) 15 | { 16 | node.Compile(context); 17 | } 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstConstantInt32.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Interfaces; 4 | 5 | namespace EmitMapper.AST.Nodes 6 | { 7 | class AstConstantInt32 : IAstValue 8 | { 9 | public Int32 value; 10 | 11 | #region IAstReturnValueNode Members 12 | 13 | public Type itemType 14 | { 15 | get { return typeof(Int32); } 16 | } 17 | 18 | #endregion 19 | 20 | #region IAstNode Members 21 | 22 | public void Compile(CompilationContext context) 23 | { 24 | context.Emit(OpCodes.Ldc_I4, value); 25 | } 26 | 27 | #endregion 28 | } 29 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstConstantNull.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Interfaces; 4 | 5 | namespace EmitMapper.AST.Nodes 6 | { 7 | class AstConstantNull : IAstRefOrValue 8 | { 9 | #region IAstReturnValueNode Members 10 | 11 | public Type itemType 12 | { 13 | get { return typeof(object); } 14 | } 15 | 16 | #endregion 17 | 18 | #region IAstNode Members 19 | 20 | public void Compile(CompilationContext context) 21 | { 22 | context.Emit(OpCodes.Ldnull); 23 | } 24 | 25 | #endregion 26 | } 27 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstConstantString.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.AST.Interfaces; 6 | using System.Reflection.Emit; 7 | 8 | namespace EmitMapper.AST.Nodes 9 | { 10 | class AstConstantString: IAstRef 11 | { 12 | public string str; 13 | 14 | #region IAstStackItem Members 15 | 16 | public Type itemType 17 | { 18 | get 19 | { 20 | return typeof(string); 21 | } 22 | } 23 | 24 | #endregion 25 | 26 | #region IAstNode Members 27 | 28 | public void Compile(CompilationContext context) 29 | { 30 | context.Emit(OpCodes.Ldstr, str); 31 | } 32 | 33 | #endregion 34 | } 35 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstExceptionHandlingBlock.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using EmitMapper.AST.Interfaces; 3 | using System.Reflection.Emit; 4 | 5 | namespace EmitMapper.AST.Nodes 6 | { 7 | class AstExceptionHandlingBlock : IAstNode 8 | { 9 | IAstNode protectedBlock; 10 | IAstNode handlerBlock; 11 | Type exceptionType; 12 | LocalBuilder eceptionVariable; 13 | 14 | public AstExceptionHandlingBlock( 15 | IAstNode protectedBlock, 16 | IAstNode handlerBlock, 17 | Type exceptionType, 18 | LocalBuilder eceptionVariable) 19 | { 20 | this.protectedBlock = protectedBlock; 21 | this.handlerBlock = handlerBlock; 22 | this.exceptionType = exceptionType; 23 | this.eceptionVariable = eceptionVariable; 24 | } 25 | 26 | #region IAstNode Members 27 | 28 | public void Compile(CompilationContext context) 29 | { 30 | var endBlock = context.ilGenerator.BeginExceptionBlock(); 31 | protectedBlock.Compile(context); 32 | context.ilGenerator.BeginCatchBlock(exceptionType); 33 | context.ilGenerator.Emit(OpCodes.Stloc, eceptionVariable); 34 | handlerBlock.Compile(context); 35 | context.ilGenerator.EndExceptionBlock(); 36 | } 37 | 38 | #endregion 39 | } 40 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstExprEquals.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Interfaces; 4 | 5 | namespace EmitMapper.AST.Nodes 6 | { 7 | class AstExprEquals : IAstValue 8 | { 9 | IAstValue leftValue; 10 | IAstValue rightValue; 11 | 12 | public AstExprEquals(IAstValue leftValue, IAstValue rightValue) 13 | { 14 | this.leftValue = leftValue; 15 | this.rightValue = rightValue; 16 | } 17 | 18 | #region IAstReturnValueNode Members 19 | 20 | public Type itemType 21 | { 22 | get { return typeof(Int32); } 23 | } 24 | 25 | #endregion 26 | 27 | #region IAstNode Members 28 | 29 | public void Compile(CompilationContext context) 30 | { 31 | leftValue.Compile(context); 32 | rightValue.Compile(context); 33 | context.Emit(OpCodes.Ceq); 34 | } 35 | 36 | #endregion 37 | } 38 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstExprIsNull.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.AST.Interfaces; 6 | using System.Reflection.Emit; 7 | using EmitMapper.Utils; 8 | using EmitMapper.AST.Helpers; 9 | 10 | namespace EmitMapper.AST.Nodes 11 | { 12 | class AstExprIsNull : IAstValue 13 | { 14 | IAstRefOrValue value; 15 | 16 | public AstExprIsNull(IAstRefOrValue value) 17 | { 18 | this.value = value; 19 | } 20 | 21 | #region IAstReturnValueNode Members 22 | 23 | public Type itemType 24 | { 25 | get { return typeof(Int32); } 26 | } 27 | 28 | #endregion 29 | 30 | #region IAstNode Members 31 | 32 | public void Compile(CompilationContext context) 33 | { 34 | if (!(value is IAstRef) && !ReflectionUtils.IsNullable(value.itemType)) 35 | { 36 | context.Emit(OpCodes.Ldc_I4_1); 37 | } 38 | else if (ReflectionUtils.IsNullable(value.itemType)) 39 | { 40 | AstBuildHelper.ReadPropertyRV( 41 | new AstValueToAddr((IAstValue)value), 42 | value.itemType.GetProperty("HasValue") 43 | ).Compile(context); 44 | context.Emit(OpCodes.Ldc_I4_0); 45 | context.Emit(OpCodes.Ceq); 46 | } 47 | else 48 | { 49 | value.Compile(context); 50 | new AstConstantNull().Compile(context); 51 | context.Emit(OpCodes.Ceq); 52 | } 53 | } 54 | 55 | #endregion 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstExprNot.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.AST.Interfaces; 6 | using System.Reflection.Emit; 7 | 8 | namespace EmitMapper.AST.Nodes 9 | { 10 | class AstExprNot : IAstValue 11 | { 12 | IAstRefOrValue _value; 13 | 14 | public Type itemType 15 | { 16 | get { return typeof(Int32); } 17 | } 18 | 19 | public AstExprNot(IAstRefOrValue value) 20 | { 21 | _value = value; 22 | } 23 | 24 | public void Compile(CompilationContext context) 25 | { 26 | context.Emit(OpCodes.Ldc_I4_0); 27 | _value.Compile(context); 28 | context.Emit(OpCodes.Ceq); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstIf.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection.Emit; 2 | using EmitMapper.AST.Interfaces; 3 | 4 | namespace EmitMapper.AST.Nodes 5 | { 6 | class AstIf: IAstNode 7 | { 8 | public IAstValue condition; 9 | public AstComplexNode trueBranch; 10 | public AstComplexNode falseBranch; 11 | 12 | #region IAstNode Members 13 | 14 | public void Compile(CompilationContext context) 15 | { 16 | Label elseLabel = context.ilGenerator.DefineLabel(); 17 | Label endIfLabel = context.ilGenerator.DefineLabel(); 18 | 19 | condition.Compile(context); 20 | context.Emit(OpCodes.Brfalse, elseLabel); 21 | 22 | if (trueBranch != null) 23 | { 24 | trueBranch.Compile(context); 25 | } 26 | if (falseBranch != null) 27 | { 28 | context.Emit(OpCodes.Br, endIfLabel); 29 | } 30 | 31 | context.ilGenerator.MarkLabel(elseLabel); 32 | if (falseBranch != null) 33 | { 34 | falseBranch.Compile(context); 35 | } 36 | context.ilGenerator.MarkLabel(endIfLabel); 37 | } 38 | 39 | #endregion 40 | } 41 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstIfNull.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.AST.Interfaces; 6 | using System.Reflection.Emit; 7 | 8 | namespace EmitMapper.AST.Nodes 9 | { 10 | /// 11 | /// Generates "value ?? ifNullValue" expression. 12 | /// 13 | class AstIfNull : IAstRefOrValue 14 | { 15 | IAstRef _value; 16 | IAstRefOrValue _ifNullValue; 17 | 18 | public Type itemType 19 | { 20 | get 21 | { 22 | return _value.itemType; 23 | } 24 | } 25 | 26 | public AstIfNull(IAstRef value, IAstRefOrValue ifNullValue) 27 | { 28 | _value = value; 29 | _ifNullValue = ifNullValue; 30 | if (!_value.itemType.IsAssignableFrom(_ifNullValue.itemType)) 31 | { 32 | throw new EmitMapperException("Incorrect ifnull expression"); 33 | } 34 | } 35 | 36 | public void Compile(CompilationContext context) 37 | { 38 | Label ifNotNullLabel = context.ilGenerator.DefineLabel(); 39 | _value.Compile(context); 40 | context.Emit(OpCodes.Dup); 41 | context.Emit(OpCodes.Brtrue_S, ifNotNullLabel); 42 | context.Emit(OpCodes.Pop); 43 | _ifNullValue.Compile(context); 44 | context.ilGenerator.MarkLabel(ifNotNullLabel); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstIfTernar.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.AST.Interfaces; 6 | using System.Reflection.Emit; 7 | 8 | namespace EmitMapper.AST.Nodes 9 | { 10 | class AstIfTernar : IAstRefOrValue 11 | { 12 | public IAstRefOrValue condition; 13 | public IAstRefOrValue trueBranch; 14 | public IAstRefOrValue falseBranch; 15 | 16 | #region IAstNode Members 17 | 18 | public Type itemType 19 | { 20 | get 21 | { 22 | return trueBranch.itemType; 23 | } 24 | } 25 | 26 | public AstIfTernar(IAstRefOrValue condition, IAstRefOrValue trueBranch, IAstRefOrValue falseBranch) 27 | { 28 | if (trueBranch.itemType != falseBranch.itemType) 29 | { 30 | throw new EmitMapperException("Types mismatch"); 31 | } 32 | 33 | this.condition = condition; 34 | this.trueBranch = trueBranch; 35 | this.falseBranch = falseBranch; 36 | } 37 | 38 | public void Compile(CompilationContext context) 39 | { 40 | Label elseLabel = context.ilGenerator.DefineLabel(); 41 | Label endIfLabel = context.ilGenerator.DefineLabel(); 42 | 43 | condition.Compile(context); 44 | context.Emit(OpCodes.Brfalse, elseLabel); 45 | 46 | if (trueBranch != null) 47 | { 48 | trueBranch.Compile(context); 49 | } 50 | if (falseBranch != null) 51 | { 52 | context.Emit(OpCodes.Br, endIfLabel); 53 | } 54 | 55 | context.ilGenerator.MarkLabel(elseLabel); 56 | if (falseBranch != null) 57 | { 58 | falseBranch.Compile(context); 59 | } 60 | context.ilGenerator.MarkLabel(endIfLabel); 61 | } 62 | 63 | #endregion 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstIndirectRead.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Helpers; 4 | using EmitMapper.AST.Interfaces; 5 | 6 | namespace EmitMapper.AST.Nodes 7 | { 8 | abstract class AstIndirectRead : IAstStackItem 9 | { 10 | public Type argumentType; 11 | 12 | public Type itemType 13 | { 14 | get 15 | { 16 | return argumentType; 17 | } 18 | } 19 | 20 | public abstract void Compile(CompilationContext context); 21 | } 22 | 23 | class AstIndirectReadRef : AstIndirectRead, IAstRef 24 | { 25 | override public void Compile(CompilationContext context) 26 | { 27 | CompilationHelper.CheckIsRef(itemType); 28 | context.Emit(OpCodes.Ldind_Ref, itemType); 29 | } 30 | } 31 | 32 | class AstIndirectReadValue : AstIndirectRead, IAstValue 33 | { 34 | override public void Compile(CompilationContext context) 35 | { 36 | CompilationHelper.CheckIsValue(itemType); 37 | if (itemType == typeof(Int32)) 38 | { 39 | context.Emit(OpCodes.Ldind_I4); 40 | } 41 | else 42 | { 43 | throw new Exception("Unsupported type"); 44 | } 45 | } 46 | } 47 | 48 | class AstIndirectReadAddr : AstIndirectRead, IAstAddr 49 | { 50 | override public void Compile(CompilationContext context) 51 | { 52 | CompilationHelper.CheckIsValue(itemType); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstInitializeLocalVariable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Interfaces; 4 | 5 | namespace EmitMapper.AST.Nodes 6 | { 7 | class AstInitializeLocalVariable: IAstNode 8 | { 9 | public Type localType; 10 | public int localIndex; 11 | 12 | public AstInitializeLocalVariable() 13 | { 14 | } 15 | 16 | public AstInitializeLocalVariable(LocalBuilder loc) 17 | { 18 | localType = loc.LocalType; 19 | localIndex = loc.LocalIndex; 20 | } 21 | 22 | public void Compile(CompilationContext context) 23 | { 24 | if(localType.IsValueType) 25 | { 26 | context.Emit(OpCodes.Ldloca, localIndex); 27 | context.Emit(OpCodes.Initobj, localType); 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstNewNullable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.AST.Interfaces; 6 | using System.Reflection.Emit; 7 | 8 | namespace EmitMapper.AST.Nodes 9 | { 10 | class AstNewNullable: IAstValue 11 | { 12 | private Type _nullableType; 13 | public Type itemType 14 | { 15 | get 16 | { 17 | return _nullableType; 18 | } 19 | } 20 | public void Compile(CompilationContext context) 21 | { 22 | throw new NotImplementedException(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstReadArgument.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Helpers; 4 | using EmitMapper.AST.Interfaces; 5 | 6 | namespace EmitMapper.AST.Nodes 7 | { 8 | class AstReadArgument : IAstStackItem 9 | { 10 | public int argumentIndex; 11 | public Type argumentType; 12 | 13 | public Type itemType 14 | { 15 | get 16 | { 17 | return argumentType; 18 | } 19 | } 20 | 21 | public virtual void Compile(CompilationContext context) 22 | { 23 | switch (argumentIndex) 24 | { 25 | case 0: 26 | context.Emit(OpCodes.Ldarg_0); 27 | break; 28 | case 1: 29 | context.Emit(OpCodes.Ldarg_1); 30 | break; 31 | case 2: 32 | context.Emit(OpCodes.Ldarg_2); 33 | break; 34 | case 3: 35 | context.Emit(OpCodes.Ldarg_3); 36 | break; 37 | default: 38 | context.Emit(OpCodes.Ldarg, argumentIndex); 39 | break; 40 | } 41 | } 42 | } 43 | 44 | class AstReadArgumentRef : AstReadArgument, IAstRef 45 | { 46 | override public void Compile(CompilationContext context) 47 | { 48 | CompilationHelper.CheckIsRef(itemType); 49 | base.Compile(context); 50 | } 51 | } 52 | 53 | class AstReadArgumentValue : AstReadArgument, IAstValue 54 | { 55 | override public void Compile(CompilationContext context) 56 | { 57 | CompilationHelper.CheckIsValue(itemType); 58 | base.Compile(context); 59 | } 60 | } 61 | 62 | class AstReadArgumentAddr : AstReadArgument, IAstAddr 63 | { 64 | override public void Compile(CompilationContext context) 65 | { 66 | CompilationHelper.CheckIsValue(itemType); 67 | context.Emit(OpCodes.Ldarga, argumentIndex); 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstReadArrayItem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Helpers; 4 | using EmitMapper.AST.Interfaces; 5 | 6 | namespace EmitMapper.AST.Nodes 7 | { 8 | class AstReadArrayItem : IAstStackItem 9 | { 10 | public IAstRef array; 11 | public int index; 12 | 13 | public Type itemType 14 | { 15 | get 16 | { 17 | return array.itemType.GetElementType(); 18 | } 19 | } 20 | 21 | public virtual void Compile(CompilationContext context) 22 | { 23 | array.Compile(context); 24 | context.Emit(OpCodes.Ldc_I4, index); 25 | context.Emit(OpCodes.Ldelem, itemType); 26 | } 27 | } 28 | 29 | class AstReadArrayItemRef : AstReadArrayItem, IAstRef 30 | { 31 | override public void Compile(CompilationContext context) 32 | { 33 | CompilationHelper.CheckIsRef(itemType); 34 | base.Compile(context); 35 | } 36 | } 37 | 38 | class AstReadArrayItemValue: AstReadArrayItem, IAstValue 39 | { 40 | override public void Compile(CompilationContext context) 41 | { 42 | CompilationHelper.CheckIsValue(itemType); 43 | base.Compile(context); 44 | } 45 | } 46 | 47 | class AstReadArrayItemAddr : AstReadArrayItem, IAstAddr 48 | { 49 | override public void Compile(CompilationContext context) 50 | { 51 | CompilationHelper.CheckIsValue(itemType); 52 | array.Compile(context); 53 | context.Emit(OpCodes.Ldc_I4, index); 54 | context.Emit(OpCodes.Ldelema, itemType); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstReadField.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Reflection.Emit; 4 | using EmitMapper.AST.Helpers; 5 | using EmitMapper.AST.Interfaces; 6 | 7 | namespace EmitMapper.AST.Nodes 8 | { 9 | class AstReadField: IAstStackItem 10 | { 11 | public IAstRefOrAddr sourceObject; 12 | public FieldInfo fieldInfo; 13 | 14 | public Type itemType 15 | { 16 | get 17 | { 18 | return fieldInfo.FieldType; 19 | } 20 | } 21 | 22 | public virtual void Compile(CompilationContext context) 23 | { 24 | sourceObject.Compile(context); 25 | context.Emit(OpCodes.Ldfld, fieldInfo); 26 | } 27 | } 28 | 29 | class AstReadFieldRef : AstReadField, IAstRef 30 | { 31 | override public void Compile(CompilationContext context) 32 | { 33 | CompilationHelper.CheckIsRef(itemType); 34 | base.Compile(context); 35 | } 36 | } 37 | 38 | class AstReadFieldValue : AstReadField, IAstValue 39 | { 40 | override public void Compile(CompilationContext context) 41 | { 42 | CompilationHelper.CheckIsValue(itemType); 43 | base.Compile(context); 44 | } 45 | } 46 | 47 | class AstReadFieldAddr : AstReadField, IAstAddr 48 | { 49 | override public void Compile(CompilationContext context) 50 | { 51 | CompilationHelper.CheckIsValue(itemType); 52 | sourceObject.Compile(context); 53 | context.Emit(OpCodes.Ldflda, fieldInfo); 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstReadLocal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Helpers; 4 | using EmitMapper.AST.Interfaces; 5 | 6 | namespace EmitMapper.AST.Nodes 7 | { 8 | class AstReadLocal : IAstStackItem 9 | { 10 | public int localIndex; 11 | public Type localType; 12 | 13 | public Type itemType 14 | { 15 | get 16 | { 17 | return localType; 18 | } 19 | } 20 | 21 | public AstReadLocal() 22 | { 23 | } 24 | 25 | public AstReadLocal(LocalBuilder loc) 26 | { 27 | localIndex = loc.LocalIndex; 28 | localType = loc.LocalType; 29 | } 30 | 31 | public virtual void Compile(CompilationContext context) 32 | { 33 | context.Emit(OpCodes.Ldloc, localIndex); 34 | } 35 | } 36 | 37 | class AstReadLocalRef : AstReadLocal, IAstRef 38 | { 39 | override public void Compile(CompilationContext context) 40 | { 41 | CompilationHelper.CheckIsRef(itemType); 42 | base.Compile(context); 43 | } 44 | } 45 | 46 | class AstReadLocalValue : AstReadLocal, IAstValue 47 | { 48 | override public void Compile(CompilationContext context) 49 | { 50 | CompilationHelper.CheckIsValue(itemType); 51 | base.Compile(context); 52 | } 53 | } 54 | 55 | class AstReadLocalAddr : AstReadLocal, IAstAddr 56 | { 57 | public AstReadLocalAddr(LocalBuilder loc) 58 | { 59 | localIndex = loc.LocalIndex; 60 | localType = loc.LocalType.MakeByRefType(); 61 | } 62 | 63 | override public void Compile(CompilationContext context) 64 | { 65 | //CompilationHelper.CheckIsValue(itemType); 66 | context.Emit(OpCodes.Ldloca, localIndex); 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstReadProperty.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using EmitMapper.AST.Helpers; 4 | using EmitMapper.AST.Interfaces; 5 | 6 | namespace EmitMapper.AST.Nodes 7 | { 8 | class AstReadProperty : IAstRefOrValue 9 | { 10 | public IAstRefOrAddr sourceObject; 11 | public PropertyInfo propertyInfo; 12 | 13 | public Type itemType 14 | { 15 | get 16 | { 17 | return propertyInfo.PropertyType; 18 | } 19 | } 20 | 21 | public virtual void Compile(CompilationContext context) 22 | { 23 | MethodInfo mi = propertyInfo.GetGetMethod(); 24 | 25 | if (mi == null) 26 | { 27 | throw new Exception("Property " + propertyInfo.Name + " doesn't have get accessor"); 28 | } 29 | 30 | AstBuildHelper.CallMethod(mi, sourceObject, null).Compile(context); 31 | } 32 | } 33 | 34 | class AstReadPropertyRef : AstReadProperty, IAstRef 35 | { 36 | override public void Compile(CompilationContext context) 37 | { 38 | CompilationHelper.CheckIsRef(itemType); 39 | base.Compile(context); 40 | } 41 | } 42 | 43 | class AstReadPropertyValue : AstReadProperty, IAstValue 44 | { 45 | override public void Compile(CompilationContext context) 46 | { 47 | CompilationHelper.CheckIsValue(itemType); 48 | base.Compile(context); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstReadThis.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using EmitMapper.AST.Helpers; 3 | using EmitMapper.AST.Interfaces; 4 | 5 | namespace EmitMapper.AST.Nodes 6 | { 7 | class AstReadThis : IAstRefOrAddr 8 | { 9 | public Type thisType; 10 | 11 | public Type itemType 12 | { 13 | get 14 | { 15 | return thisType; 16 | } 17 | } 18 | 19 | public AstReadThis() 20 | { 21 | } 22 | 23 | public virtual void Compile(CompilationContext context) 24 | { 25 | AstReadArgument arg = new AstReadArgument() 26 | { 27 | argumentIndex = 0, 28 | argumentType = thisType 29 | }; 30 | arg.Compile(context); 31 | } 32 | } 33 | 34 | class AstReadThisRef : AstReadThis, IAstRef 35 | { 36 | override public void Compile(CompilationContext context) 37 | { 38 | CompilationHelper.CheckIsRef(itemType); 39 | base.Compile(context); 40 | } 41 | } 42 | 43 | class AstReadThisAddr : AstReadThis, IAstRef 44 | { 45 | override public void Compile(CompilationContext context) 46 | { 47 | CompilationHelper.CheckIsRef(itemType); 48 | base.Compile(context); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstReturn.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Helpers; 4 | using EmitMapper.AST.Interfaces; 5 | 6 | namespace EmitMapper.AST.Nodes 7 | { 8 | class AstReturn : IAstNode, IAstAddr 9 | { 10 | public Type returnType; 11 | public IAstRefOrValue returnValue; 12 | 13 | public void Compile(CompilationContext context) 14 | { 15 | returnValue.Compile(context); 16 | CompilationHelper.PrepareValueOnStack(context, returnType, returnValue.itemType); 17 | context.Emit(OpCodes.Ret); 18 | } 19 | 20 | public Type itemType 21 | { 22 | get { return returnType; } 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstReturnVoid.cs: -------------------------------------------------------------------------------- 1 | using EmitMapper.AST.Interfaces; 2 | using System.Reflection.Emit; 3 | 4 | namespace EmitMapper.AST.Nodes 5 | { 6 | class AstReturnVoid:IAstNode 7 | { 8 | #region IAstNode Members 9 | 10 | public void Compile(CompilationContext context) 11 | { 12 | context.Emit(OpCodes.Ret); 13 | } 14 | 15 | #endregion 16 | } 17 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstThrow.cs: -------------------------------------------------------------------------------- 1 | using EmitMapper.AST.Interfaces; 2 | using System.Reflection.Emit; 3 | 4 | namespace EmitMapper.AST.Nodes 5 | { 6 | class AstThrow: IAstNode 7 | { 8 | public IAstRef exception; 9 | 10 | public void Compile(CompilationContext context) 11 | { 12 | exception.Compile(context); 13 | context.Emit(OpCodes.Throw); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstTypeof.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.AST.Interfaces; 6 | using EmitMapper.AST.Helpers; 7 | using System.Reflection.Emit; 8 | 9 | namespace EmitMapper.AST.Nodes 10 | { 11 | class AstTypeof: IAstRef 12 | { 13 | public Type type; 14 | 15 | #region IAstStackItem Members 16 | 17 | public Type itemType 18 | { 19 | get 20 | { 21 | return typeof(Type); 22 | } 23 | } 24 | 25 | #endregion 26 | 27 | #region IAstNode Members 28 | 29 | public void Compile(CompilationContext context) 30 | { 31 | context.Emit(OpCodes.Ldtoken, type); 32 | context.EmitCall(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle")); 33 | } 34 | 35 | #endregion 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstUnbox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Interfaces; 4 | 5 | namespace EmitMapper.AST.Nodes 6 | { 7 | class AstUnbox : IAstValue 8 | { 9 | public Type unboxedType; 10 | public IAstRef refObj; 11 | 12 | public Type itemType 13 | { 14 | get { return unboxedType; } 15 | } 16 | 17 | public void Compile(CompilationContext context) 18 | { 19 | refObj.Compile(context); 20 | context.Emit(OpCodes.Unbox_Any, unboxedType); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstValueToAddr.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Interfaces; 4 | 5 | namespace EmitMapper.AST.Nodes 6 | { 7 | class AstValueToAddr: IAstAddr 8 | { 9 | public IAstValue value; 10 | public Type itemType 11 | { 12 | get 13 | { 14 | return value.itemType; 15 | } 16 | } 17 | 18 | public AstValueToAddr(IAstValue value) 19 | { 20 | this.value = value; 21 | } 22 | 23 | public void Compile(CompilationContext context) 24 | { 25 | LocalBuilder loc = context.ilGenerator.DeclareLocal(itemType); 26 | new AstInitializeLocalVariable(loc).Compile(context); 27 | new AstWriteLocal() 28 | { 29 | localIndex = loc.LocalIndex, 30 | localType = loc.LocalType, 31 | value = value 32 | }.Compile(context); 33 | new AstReadLocalAddr(loc).Compile(context); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstWriteField.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Helpers; 4 | using EmitMapper.AST.Interfaces; 5 | 6 | namespace EmitMapper.AST.Nodes 7 | { 8 | class AstWriteField: IAstNode 9 | { 10 | public IAstRefOrAddr targetObject; 11 | public IAstRefOrValue value; 12 | public FieldInfo fieldInfo; 13 | 14 | public void Compile(CompilationContext context) 15 | { 16 | targetObject.Compile(context); 17 | value.Compile(context); 18 | CompilationHelper.PrepareValueOnStack(context, fieldInfo.FieldType, value.itemType); 19 | context.Emit(OpCodes.Stfld, fieldInfo); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstWriteLocal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using EmitMapper.AST.Helpers; 4 | using EmitMapper.AST.Interfaces; 5 | 6 | namespace EmitMapper.AST.Nodes 7 | { 8 | class AstWriteLocal : IAstNode 9 | { 10 | public int localIndex; 11 | public Type localType; 12 | public IAstRefOrValue value; 13 | 14 | public AstWriteLocal() 15 | { 16 | } 17 | 18 | public AstWriteLocal(LocalBuilder loc, IAstRefOrValue value) 19 | { 20 | localIndex = loc.LocalIndex; 21 | localType = loc.LocalType; 22 | this.value = value; 23 | } 24 | 25 | 26 | public void Compile(CompilationContext context) 27 | { 28 | value.Compile(context); 29 | CompilationHelper.PrepareValueOnStack(context, localType, value.itemType); 30 | context.Emit(OpCodes.Stloc, localIndex); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /EmitMapper/AST/Nodes/AstWriteProperty.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using EmitMapper.AST.Helpers; 5 | using EmitMapper.AST.Interfaces; 6 | 7 | namespace EmitMapper.AST.Nodes 8 | { 9 | class AstWriteProperty : IAstNode 10 | { 11 | private IAstRefOrAddr _targetObject; 12 | private IAstRefOrValue _value; 13 | private PropertyInfo _propertyInfo; 14 | private MethodInfo _setMethod; 15 | 16 | public AstWriteProperty(IAstRefOrAddr targetObject, IAstRefOrValue value, PropertyInfo propertyInfo) 17 | { 18 | _targetObject = targetObject; 19 | _value = value; 20 | _propertyInfo = propertyInfo; 21 | _setMethod = propertyInfo.GetSetMethod(); 22 | if (_setMethod == null) 23 | { 24 | throw new Exception("Property " + propertyInfo.Name + " doesn't have set accessor"); 25 | } 26 | if (_setMethod.GetParameters().Length != 1) 27 | { 28 | throw new EmitMapperException("Property " + propertyInfo.Name + " has invalid arguments"); 29 | } 30 | } 31 | 32 | public void Compile(CompilationContext context) 33 | { 34 | AstBuildHelper.CallMethod( 35 | _setMethod, 36 | _targetObject, 37 | new List() { _value } 38 | ).Compile(context); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /EmitMapper/AbstractTypeBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper 7 | { 8 | static class AbstractTypeBuilder 9 | { 10 | private static Dictionary _typesCache; 11 | 12 | public static Type BuildType(Type type) 13 | { 14 | var typeBuilder = DynamicAssemblyManager.DefineType(type.ToString() + "_" 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /EmitMapper/DynamicAssemblyManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | using System.Reflection; 4 | using EmitMapper.Mappers; 5 | 6 | namespace EmitMapper 7 | { 8 | /// 9 | /// Class which maintains an assembly for created object Mappers 10 | /// 11 | public class DynamicAssemblyManager 12 | { 13 | /// 14 | /// Saves assembly with created Mappers to file. This method is useful for debugging purpose. 15 | /// 16 | public static void SaveAssembly() 17 | { 18 | #if !SILVERLIGHT 19 | lock (typeof(DynamicAssemblyManager)) 20 | { 21 | assemblyBuilder.Save(assemblyName.Name + ".dll"); 22 | } 23 | #else 24 | throw new NotImplementedException("DynamicAssemblyManager.SaveAssembly"); 25 | #endif 26 | } 27 | 28 | #region Non-public members 29 | 30 | private static AssemblyName assemblyName; 31 | private static AssemblyBuilder assemblyBuilder; 32 | private static ModuleBuilder moduleBuilder; 33 | 34 | static DynamicAssemblyManager() 35 | { 36 | #if !SILVERLIGHT 37 | assemblyName = new AssemblyName("EmitMapperAssembly"); 38 | assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( 39 | assemblyName, 40 | AssemblyBuilderAccess.RunAndSave 41 | ); 42 | 43 | moduleBuilder = assemblyBuilder.DefineDynamicModule( 44 | assemblyName.Name, 45 | assemblyName.Name + ".dll", 46 | true); 47 | #else 48 | assemblyName = new AssemblyName("EmitMapperAssembly.SL"); 49 | assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( 50 | assemblyName, 51 | AssemblyBuilderAccess.Run 52 | ); 53 | moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, true); 54 | #endif 55 | } 56 | 57 | private static string CorrectTypeName(string typeName) 58 | { 59 | if (typeName.Length >= 1042) 60 | { 61 | typeName = "type_" + typeName.Substring(0, 900) + Guid.NewGuid().ToString().Replace("-", ""); 62 | } 63 | return typeName; 64 | } 65 | 66 | internal static TypeBuilder DefineMapperType(string typeName) 67 | { 68 | lock (typeof(DynamicAssemblyManager)) 69 | { 70 | return moduleBuilder.DefineType( 71 | CorrectTypeName(typeName + Guid.NewGuid().ToString().Replace("-", "")), 72 | TypeAttributes.Public, 73 | typeof(MapperForClassImpl), 74 | null 75 | ); 76 | } 77 | } 78 | 79 | internal static TypeBuilder DefineType(string typeName, Type parent) 80 | { 81 | lock (typeof(DynamicAssemblyManager)) 82 | { 83 | return moduleBuilder.DefineType( 84 | CorrectTypeName(typeName), 85 | TypeAttributes.Public, 86 | parent, 87 | null 88 | ); 89 | } 90 | } 91 | #endregion 92 | } 93 | } -------------------------------------------------------------------------------- /EmitMapper/EmitBuilders/BuilderUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.AST.Interfaces; 6 | using EmitMapper.AST.Nodes; 7 | using EmitMapper.AST.Helpers; 8 | using System.Reflection.Emit; 9 | 10 | namespace EmitMapper.EmitBuilders 11 | { 12 | class BuilderUtils 13 | { 14 | /// 15 | /// Copies an argument to local variable 16 | /// 17 | /// 18 | /// 19 | /// 20 | public static IAstNode InitializeLocal(LocalBuilder loc, int argIndex) 21 | { 22 | return new AstComplexNode() 23 | { 24 | nodes = 25 | new List() 26 | { 27 | new AstInitializeLocalVariable(loc), 28 | new AstWriteLocal() 29 | { 30 | localIndex = loc.LocalIndex, 31 | localType = loc.LocalType, 32 | value = AstBuildHelper.ReadArgumentRV(argIndex, typeof(object)) 33 | } 34 | } 35 | }; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /EmitMapper/EmitBuilders/CreateTargetInstanceBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using EmitMapper.AST; 5 | using EmitMapper.AST.Helpers; 6 | using EmitMapper.AST.Interfaces; 7 | using EmitMapper.AST.Nodes; 8 | using EmitMapper.Mappers; 9 | using EmitMapper.Utils; 10 | using System.Reflection.Emit; 11 | using System.Reflection; 12 | using EmitMapper.MappingConfiguration.MappingOperations; 13 | 14 | namespace EmitMapper 15 | { 16 | class CreateTargetInstanceBuilder 17 | { 18 | public static void BuildCreateTargetInstanceMethod(Type type, TypeBuilder typeBuilder) 19 | { 20 | if (ReflectionUtils.IsNullable(type)) 21 | { 22 | type = Nullable.GetUnderlyingType(type); 23 | } 24 | 25 | MethodBuilder methodBuilder = typeBuilder.DefineMethod( 26 | "CreateTargetInstance", 27 | MethodAttributes.Assembly | MethodAttributes.Virtual, 28 | typeof(object), 29 | null 30 | ); 31 | 32 | ILGenerator ilGen = methodBuilder.GetILGenerator(); 33 | CompilationContext context = new CompilationContext(ilGen); 34 | IAstRefOrValue returnValue; 35 | 36 | if (type.IsValueType) 37 | { 38 | LocalBuilder lb = ilGen.DeclareLocal(type); 39 | new AstInitializeLocalVariable(lb).Compile(context); 40 | 41 | returnValue = 42 | new AstBox() 43 | { 44 | value = AstBuildHelper.ReadLocalRV(lb) 45 | }; 46 | } 47 | else 48 | { 49 | returnValue = 50 | ReflectionUtils.HasDefaultConstructor(type) 51 | ? 52 | new AstNewObject() 53 | { 54 | objectType = type 55 | } 56 | : 57 | (IAstRefOrValue)new AstConstantNull(); 58 | } 59 | new AstReturn() 60 | { 61 | returnType = type, 62 | returnValue = returnValue 63 | }.Compile(context); 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Delegates/DelegateInvokerAction_0.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class DelegateInvokerAction_0 : DelegateInvokerBase 9 | { 10 | public abstract void CallAction(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Delegates/DelegateInvokerAction_1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class DelegateInvokerAction_1 : DelegateInvokerBase 9 | { 10 | public abstract void CallAction(object param1); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Delegates/DelegateInvokerAction_2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class DelegateInvokerAction_2 : DelegateInvokerBase 9 | { 10 | public abstract void CallAction(object param1, object param2); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Delegates/DelegateInvokerAction_3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class DelegateInvokerAction_3 : DelegateInvokerBase 9 | { 10 | public abstract void CallAction(object param1, object param2, object param3); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Delegates/DelegateInvokerBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public class DelegateInvokerBase 9 | { 10 | internal Delegate _del; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Delegates/DelegateInvokerFunc_0.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class DelegateInvokerFunc_0 : DelegateInvokerBase 9 | { 10 | public abstract object CallFunc(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Delegates/DelegateInvokerFunc_1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class DelegateInvokerFunc_1 : DelegateInvokerBase 9 | { 10 | public abstract object CallFunc(object param1); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Delegates/DelegateInvokerFunc_2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class DelegateInvokerFunc_2 : DelegateInvokerBase 9 | { 10 | public abstract object CallFunc(object param1, object param2); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Delegates/DelegateInvokerFunc_3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class DelegateInvokerFunc_3 : DelegateInvokerBase 9 | { 10 | public abstract object CallFunc(object param1, object param2, object param3); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Methods/MethodInvokerAction_0.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class MethodInvokerAction_0 : MethodInvokerBase 9 | { 10 | public abstract void CallAction(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Methods/MethodInvokerAction_1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class MethodInvokerAction_1 : MethodInvokerBase 9 | { 10 | public abstract void CallAction(object param1); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Methods/MethodInvokerAction_2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class MethodInvokerAction_2 : MethodInvokerBase 9 | { 10 | public abstract void CallAction(object param1, object param2); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Methods/MethodInvokerAction_3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class MethodInvokerAction_3 : MethodInvokerBase 9 | { 10 | public abstract void CallAction(object param1, object param2, object param3); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Methods/MethodInvokerBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public class MethodInvokerBase 9 | { 10 | internal object targetObject; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Methods/MethodInvokerFunc_0.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class MethodInvokerFunc_0 : MethodInvokerBase 9 | { 10 | public abstract object CallFunc(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Methods/MethodInvokerFunc_1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class MethodInvokerFunc_1 : MethodInvokerBase 9 | { 10 | public abstract object CallFunc(object param1); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Methods/MethodInvokerFunc_2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class MethodInvokerFunc_2 : MethodInvokerBase 9 | { 10 | public abstract object CallFunc(object param1, object param2); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitInvoker/Methods/MethodInvokerFunc_3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.EmitInvoker 7 | { 8 | public abstract class MethodInvokerFunc_3 : MethodInvokerBase 9 | { 10 | public abstract object CallFunc(object param1, object param2, object param3); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/EmitMapperException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using EmitMapper.MappingConfiguration.MappingOperations; 3 | 4 | namespace EmitMapper 5 | { 6 | public class EmitMapperException: ApplicationException 7 | { 8 | public IMappingOperation _mappingOperation = null; 9 | public EmitMapperException() 10 | { 11 | } 12 | 13 | public EmitMapperException(string message) 14 | : base(message) 15 | { 16 | } 17 | 18 | public EmitMapperException(string message, Exception innerException) 19 | : base(message, innerException) 20 | { 21 | } 22 | 23 | public EmitMapperException(string message, Exception innerException, IMappingOperation mappingOperation) 24 | : base( 25 | BuildMessage(message, mappingOperation), 26 | innerException 27 | ) 28 | { 29 | _mappingOperation = mappingOperation; 30 | } 31 | 32 | private static string BuildMessage(string message, IMappingOperation mappingOperation) 33 | { 34 | return message + " " + mappingOperation.ToString(); 35 | } 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /EmitMapper/Mappers/CustomMapperImpl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using EmitMapper.MappingConfiguration.MappingOperations; 3 | 4 | namespace EmitMapper.Mappers 5 | { 6 | internal abstract class CustomMapperImpl: ObjectsMapperBaseImpl 7 | { 8 | public CustomMapperImpl( 9 | ObjectMapperManager mapperMannager, 10 | Type TypeFrom, 11 | Type TypeTo, 12 | IMappingConfigurator mappingConfigurator, 13 | object[] storedObjects) 14 | { 15 | Initialize(mapperMannager, TypeFrom, TypeTo, mappingConfigurator, storedObjects); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /EmitMapper/Mappers/MapperForClassImpl.cs: -------------------------------------------------------------------------------- 1 | namespace EmitMapper.Mappers 2 | { 3 | /// 4 | /// Mapper for classes 5 | /// 6 | internal abstract class MapperForClassImpl : ObjectsMapperBaseImpl 7 | { 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /EmitMapper/Mappers/MapperPrimitiveImpl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using EmitMapper.Utils; 5 | using EmitMapper.Mappers; 6 | using EmitMapper; 7 | using EmitMapper.EmitInvoker; 8 | using System.Collections; 9 | 10 | namespace EmitObjectMapper.Mappers 11 | { 12 | /// 13 | /// Mapper for primitive objects 14 | /// 15 | internal class MapperPrimitiveImpl : CustomMapperImpl 16 | { 17 | private MethodInvokerFunc_1 _converter = null; 18 | public MapperPrimitiveImpl(ObjectMapperManager mapperMannager, Type TypeFrom, Type TypeTo, IMappingConfigurator mappingConfigurator) 19 | : base(mapperMannager, TypeFrom, TypeTo, mappingConfigurator, null) 20 | { 21 | var to = TypeTo == typeof(IEnumerable) ? typeof(object) : TypeTo; 22 | var from = TypeFrom == typeof(IEnumerable) ? typeof(object) : TypeFrom; 23 | 24 | var staticConv = mappingConfigurator.GetStaticConvertersManager() ?? StaticConvertersManager.DefaultInstance; 25 | var converterMethod = staticConv.GetStaticConverter(from, to); 26 | 27 | if (converterMethod != null) 28 | { 29 | _converter = (MethodInvokerFunc_1)MethodInvoker.GetMethodInvoker( 30 | null, 31 | converterMethod 32 | ); 33 | } 34 | } 35 | 36 | /// 37 | /// Copies object properties and members of "from" to object "to" 38 | /// 39 | /// Source object 40 | /// Destination object 41 | /// Destination object 42 | internal override object MapImpl(object from, object to, object state) 43 | { 44 | if (_converter == null) 45 | { 46 | return from; 47 | } 48 | return _converter.CallFunc(from); 49 | } 50 | 51 | /// 52 | /// Creates an instance of destination object 53 | /// 54 | /// Destination object 55 | internal override object CreateTargetInstance() 56 | { 57 | return null; 58 | } 59 | 60 | internal static bool IsSupportedType(Type type) 61 | { 62 | return 63 | type.IsPrimitive 64 | || type == typeof(decimal) 65 | || type == typeof(float) 66 | || type == typeof(double) 67 | || type == typeof(long) 68 | || type == typeof(ulong) 69 | || type == typeof(short) 70 | || type == typeof(Guid) 71 | || type == typeof(string) 72 | || ReflectionUtils.IsNullable(type) && IsSupportedType(Nullable.GetUnderlyingType(type)) 73 | || type.IsEnum; 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/CustomConverterDescriptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Reflection; 6 | 7 | namespace EmitMapper.MappingConfiguration 8 | { 9 | /// 10 | /// Detailed description of a generic converter. 11 | /// 12 | public class CustomConverterDescriptor 13 | { 14 | /// 15 | /// Type of class which performs conversion. This class can be generic which will be parameterized with types 16 | /// returned from "ConverterClassTypeArguments" property. 17 | /// 18 | public Type ConverterImplementation { get; set; } 19 | 20 | /// 21 | /// Name of conversion method of class returned from "ConverterImplementation" property. 22 | /// 23 | public string ConversionMethodName { get; set; } 24 | 25 | /// 26 | /// Type arguments for parameterizing generic converter determined by "ConverterImplementation" property. 27 | /// 28 | public Type[] ConverterClassTypeArguments { get; set; } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/CustomMapConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.MappingConfiguration.MappingOperations; 6 | 7 | namespace EmitMapper.MappingConfiguration 8 | { 9 | public class CustomMapConfig : IMappingConfigurator 10 | { 11 | public Func GetMappingOperationFunc { get; set; } 12 | public string ConfigurationName { get; set; } 13 | 14 | #region IMappingConfigurator Members 15 | 16 | public IMappingOperation[] GetMappingOperations(Type from, Type to) 17 | { 18 | if (GetMappingOperationFunc == null) 19 | { 20 | return new IMappingOperation[0]; 21 | } 22 | return GetMappingOperationFunc(from, to); 23 | } 24 | 25 | public string GetConfigurationName() 26 | { 27 | return ConfigurationName; 28 | } 29 | 30 | public IRootMappingOperation GetRootMappingOperation(Type from, Type to) 31 | { 32 | return null; 33 | } 34 | 35 | 36 | #endregion 37 | 38 | 39 | #region IMappingConfigurator Members 40 | 41 | 42 | public StaticConvertersManager GetStaticConvertersManager() 43 | { 44 | return null; 45 | } 46 | 47 | #endregion 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/DefaultCustomConverterProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration 7 | { 8 | public class DefaultCustomConverterProvider: ICustomConverterProvider 9 | { 10 | private Type _converterType; 11 | 12 | public DefaultCustomConverterProvider(Type converterType) 13 | { 14 | _converterType = converterType; 15 | } 16 | 17 | public virtual CustomConverterDescriptor GetCustomConverterDescr( 18 | Type from, 19 | Type to, 20 | MapConfigBaseImpl mappingConfig) 21 | { 22 | return new CustomConverterDescriptor 23 | { 24 | ConverterClassTypeArguments = GetGenericArguments(from).Concat(GetGenericArguments(to)).ToArray(), 25 | ConverterImplementation = _converterType, 26 | ConversionMethodName = "Convert" 27 | }; 28 | } 29 | 30 | public static Type[] GetGenericArguments(Type type) 31 | { 32 | if (type.IsArray) 33 | { 34 | return new[] { type.GetElementType() }; 35 | } 36 | if (type.IsGenericType) 37 | { 38 | return type.GetGenericArguments(); 39 | } 40 | return type 41 | .GetInterfaces() 42 | .Where(i => i.IsGenericType) 43 | .Select(i => i.GetGenericArguments()) 44 | .Where(a => a.Length == 1) 45 | .Select(a => a[0]) 46 | .ToArray(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/ICustomConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration 7 | { 8 | public interface ICustomConverter 9 | { 10 | void Initialize(Type from, Type to, MapConfigBaseImpl mappingConfig); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/ICustomConverterProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration 7 | { 8 | /// 9 | /// Provider for getting detailed information about generic conversion 10 | /// 11 | public interface ICustomConverterProvider 12 | { 13 | /// 14 | /// Getting detailed information about generic conversion 15 | /// 16 | /// Type of source. Can be also generic class or abstract array. 17 | /// Type of destination. Can be also generic class or abstract array. 18 | /// Current mapping configuration 19 | /// 20 | CustomConverterDescriptor GetCustomConverterDescr(Type from, Type to, MapConfigBaseImpl mappingConfig); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/IMappingConfigurator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.MappingConfiguration.MappingOperations; 6 | 7 | namespace EmitMapper 8 | { 9 | public interface IMappingConfigurator 10 | { 11 | /// 12 | /// Get list of mapping operations. Each mapping mapping defines one copieng operation from source to destination. For this operation can be additionally defined the following custom operations: 13 | /// - Custom getter which extracts values from source 14 | /// - Custom values converter which converts extracted from source value 15 | /// - Custom setter which writes value to destination 16 | /// 17 | /// Source type 18 | /// Destination type 19 | /// 20 | IMappingOperation[] GetMappingOperations(Type from, Type to); 21 | 22 | IRootMappingOperation GetRootMappingOperation(Type from, Type to); 23 | 24 | /// 25 | /// Get unique configuration name to force Emit Mapper create new mapper instead using appropriate cached one. 26 | /// 27 | /// 28 | string GetConfigurationName(); 29 | 30 | StaticConvertersManager GetStaticConvertersManager(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/DestSrcReadOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public delegate void ValueProcessor(object srcValue, object dstValue, object state); 9 | public class DestSrcReadOperation: IDestReadOperation, ISrcReadOperation 10 | { 11 | public MemberDescriptor Destination { get; set; } 12 | public MemberDescriptor Source { get; set; } 13 | public ValueProcessor ValueProcessor { get; set; } 14 | 15 | public override string ToString() 16 | { 17 | return "DestSrcReadOperation. Source member:" + Source + " Target member:" + Destination.ToString(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/DestWriteOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public struct ValueToWrite 9 | { 10 | public enum Actions : int 11 | { 12 | write = 0, 13 | skip = 1 14 | } 15 | 16 | public T value; 17 | public Actions action; 18 | 19 | public static ValueToWrite ReturnValue(T value) 20 | { 21 | return new ValueToWrite { action = Actions.write, value = value }; 22 | } 23 | public static ValueToWrite Skip() 24 | { 25 | return new ValueToWrite { action = Actions.skip }; 26 | } 27 | } 28 | 29 | public delegate ValueToWrite ValueGetter(object value, object state); 30 | 31 | public class DestWriteOperation : IDestWriteOperation 32 | { 33 | public Delegate Getter { get; set; } 34 | public MemberDescriptor Destination { get; set; } 35 | 36 | public override string ToString() 37 | { 38 | return "DestWriteOperation. Target member:" + Destination.ToString(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/Interfaces/IComplexOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | interface IComplexOperation: IMappingOperation 9 | { 10 | List Operations { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/Interfaces/IDestOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public interface IDestOperation : IMappingOperation 9 | { 10 | MemberDescriptor Destination { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/Interfaces/IDestReadOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public interface IDestReadOperation : IDestOperation 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/Interfaces/IDestWriteOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public interface IDestWriteOperation: IDestOperation 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/Interfaces/IMappingOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public interface IMappingOperation 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/Interfaces/IReadWriteOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | interface IReadWriteOperation : IDestWriteOperation, ISrcReadOperation 9 | { 10 | Delegate NullSubstitutor { get; set; } // generic type: NullSubstitutor 11 | Delegate TargetConstructor { get; set; } // generic type: TargetConstructor 12 | Delegate Converter { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/Interfaces/IRootMappingOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public interface IRootMappingOperation: IMappingOperation 9 | { 10 | Type From { get; set; } 11 | Type To { get; set; } 12 | Delegate NullSubstitutor { get; set; } 13 | Delegate TargetConstructor { get; set; } 14 | Delegate Converter { get; set; } 15 | bool ShallowCopy { get; set; } 16 | Delegate ValuesPostProcessor { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/Interfaces/ISrcOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public interface ISrcOperation : IMappingOperation 9 | { 10 | MemberDescriptor Source { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/Interfaces/ISrcReadOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public interface ISrcReadOperation : ISrcOperation 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/Interfaces/ISrcWriteOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public interface ISrcWriteOperation : ISrcOperation 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/MappingOperationDelegates.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public delegate TResult NullSubstitutor(object state); 9 | public delegate TResult TargetConstructor(); 10 | public delegate TResult ValueConverter(TValue value, object state); 11 | public delegate TValue ValuesPostProcessor(TValue value, object state); 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/OperationsBlock.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public class OperationsBlock: IComplexOperation 9 | { 10 | public List Operations { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/ReadWriteComplex.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | 9 | /// 10 | /// Generates the following code: 11 | /// var tempSrc = Source.member; 12 | /// if(tempSrc == null) 13 | /// { 14 | /// Destination.member = null; 15 | /// } 16 | /// else 17 | /// { 18 | /// var tempDst = Destination.member; 19 | /// if(tempDst == null) 20 | /// { 21 | /// tempDst = new DestinationMemberType(); 22 | /// } 23 | /// // Operations: 24 | /// tempDst.fld1 = tempSrc.fld1; 25 | /// tempDst.fld2 = tempSrc.fld2; 26 | /// ... 27 | /// Destination.member = tempDst; 28 | /// } 29 | /// 30 | public class ReadWriteComplex : IComplexOperation, IReadWriteOperation 31 | { 32 | public bool ShallowCopy { get; set; } 33 | public MemberDescriptor Source { get; set; } 34 | public MemberDescriptor Destination { get; set; } 35 | public Delegate Converter { get; set; } 36 | public List Operations { get; set; } 37 | public Delegate NullSubstitutor { get; set; } 38 | public Delegate TargetConstructor { get; set; } 39 | public Delegate ValuesPostProcessor { get; set; } 40 | 41 | public override string ToString() 42 | { 43 | return "ReadWriteComplex. Source member:" + Source + " Target member:" + Destination.ToString(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/ReadWriteSimple.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public class ReadWriteSimple : IReadWriteOperation 9 | { 10 | public bool ShallowCopy { get; set; } 11 | public MemberDescriptor Source { get; set; } 12 | public MemberDescriptor Destination { get; set; } 13 | public Delegate NullSubstitutor { get; set; } 14 | public Delegate TargetConstructor { get; set; } 15 | public Delegate Converter { get; set; } 16 | 17 | public override string ToString() 18 | { 19 | return "ReadWriteSimple. Source member:" + Source + " Target member:" + Destination.ToString(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/RootMappingOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.MappingConfiguration.MappingOperations; 6 | 7 | namespace EmitMapper.MappingConfiguration.MappingOperations 8 | { 9 | public class RootMappingOperation: IRootMappingOperation 10 | { 11 | #region IRootMappingOperation Members 12 | public Type From { get; set; } 13 | public Type To { get; set; } 14 | public Delegate NullSubstitutor { get; set; } 15 | public Delegate TargetConstructor { get; set; } 16 | public Delegate Converter { get; set; } 17 | public bool ShallowCopy { get; set; } 18 | public Delegate ValuesPostProcessor { get; set; } 19 | #endregion 20 | 21 | public RootMappingOperation(Type from, Type to) 22 | { 23 | From = from; 24 | To = to; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MappingOperations/SrcReadOperation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.MappingConfiguration.MappingOperations 7 | { 8 | public delegate void ValueSetter(object obj, object value, object state); 9 | public class SrcReadOperation : ISrcReadOperation 10 | { 11 | public ValueSetter Setter { get; set; } 12 | public MemberDescriptor Source { get; set; } 13 | 14 | public override string ToString() 15 | { 16 | return "SrcReadOperation. Source member:" + Source.ToString(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /EmitMapper/MappingConfiguration/MemberDescriptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Reflection; 6 | using EmitMapper.Utils; 7 | 8 | namespace EmitMapper.MappingConfiguration 9 | { 10 | public class MemberDescriptor 11 | { 12 | public MemberDescriptor(MemberInfo singleMember) 13 | { 14 | MembersChain = new[] { singleMember }; 15 | } 16 | 17 | public MemberDescriptor(MemberInfo[] membersChain) 18 | { 19 | MembersChain = membersChain; 20 | } 21 | 22 | public MemberInfo[] MembersChain { get; set; } 23 | 24 | public MemberInfo MemberInfo 25 | { 26 | get 27 | { 28 | return MembersChain == null || MembersChain.Length == 0 ? null : MembersChain[MembersChain.Length - 1]; 29 | } 30 | } 31 | 32 | public Type MemberType 33 | { 34 | get 35 | { 36 | return ReflectionUtils.GetMemberType(MemberInfo); 37 | } 38 | } 39 | 40 | public override string ToString() 41 | { 42 | return "[" + MembersChain.Select(mc => ReflectionUtils.GetMemberType(mc).Name + ":" + mc.Name).ToCSV(",") + "]"; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /EmitMapper/ObjectsMapper.cs: -------------------------------------------------------------------------------- 1 | using EmitMapper.Mappers; 2 | using System.Collections.Generic; 3 | 4 | namespace EmitMapper 5 | { 6 | public class ObjectsMapper 7 | { 8 | public TTo Map(TFrom from, TTo to, object state) 9 | { 10 | return (TTo)MapperImpl.Map(from, to, state); 11 | } 12 | 13 | public TTo Map(TFrom from, TTo to) 14 | { 15 | return (TTo)MapperImpl.Map(from, to, null); 16 | } 17 | 18 | public TTo Map(TFrom from) 19 | { 20 | return (TTo)MapperImpl.Map(from); 21 | } 22 | 23 | public TTo MapUsingState(TFrom from, object state) 24 | { 25 | return (TTo)MapperImpl.Map(from, null, state); 26 | } 27 | 28 | 29 | public ObjectsMapper(ObjectsMapperBaseImpl mapperImpl) 30 | { 31 | MapperImpl = mapperImpl; 32 | } 33 | 34 | public IEnumerable MapEnum(IEnumerable sourceCollection) 35 | { 36 | foreach (TFrom src in sourceCollection) 37 | { 38 | yield return Map(src); 39 | } 40 | } 41 | 42 | public ObjectsMapperBaseImpl MapperImpl { get; set; } 43 | } 44 | } -------------------------------------------------------------------------------- /EmitMapper/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("ObjectsMapper")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ObjectsMapper")] 13 | [assembly: AssemblyCopyright("Copyright © 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("d0785576-1ec0-4fc8-835a-be5e6c8c4038")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Revision and Build Numbers 33 | // by using the '*' as shown below: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | [assembly: InternalsVisibleTo("EmitMapperAssembly")] -------------------------------------------------------------------------------- /EmitMapper/Utils/MiscUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.Utils 7 | { 8 | static class MiscUtils 9 | { 10 | public static string ToCSV(this IEnumerable collection, string delim) 11 | { 12 | if (collection == null) 13 | { 14 | return ""; 15 | } 16 | 17 | StringBuilder result = new StringBuilder(); 18 | foreach (T value in collection) 19 | { 20 | result.Append(value); 21 | result.Append(delim); 22 | } 23 | if (result.Length > 0) 24 | { 25 | result.Length -= delim.Length; 26 | } 27 | return result.ToString(); 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /EmitMapper/Utils/ReflectionUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Linq; 4 | using EmitMapper.MappingConfiguration; 5 | using System.Collections.Generic; 6 | 7 | namespace EmitMapper.Utils 8 | { 9 | public class ReflectionUtils 10 | { 11 | public class MatchedMember 12 | { 13 | public MemberInfo First { get; set; } 14 | public MemberInfo Second { get; set; } 15 | public MatchedMember(MemberInfo first, MemberInfo second) 16 | { 17 | First = first; 18 | Second = second; 19 | } 20 | } 21 | 22 | public static bool IsNullable(Type type) 23 | { 24 | return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); 25 | } 26 | 27 | public static MemberInfo[] GetPublicFieldsAndProperties(Type type) 28 | { 29 | return type 30 | .GetMembers(BindingFlags.Instance | BindingFlags.Public) 31 | .Where(mi => mi.MemberType == MemberTypes.Property || mi.MemberType == MemberTypes.Field) 32 | .ToArray(); 33 | } 34 | 35 | public static MatchedMember[] GetCommonMembers(Type first, Type second, Func matcher) 36 | { 37 | if(matcher == null) 38 | { 39 | matcher = (f, s) => f == s; 40 | } 41 | var firstMembers = GetPublicFieldsAndProperties(first); 42 | var secondMembers = GetPublicFieldsAndProperties(first); 43 | var result = new List(); 44 | foreach (var f in firstMembers) 45 | { 46 | var s = secondMembers.FirstOrDefault(sm => matcher(f.Name, sm.Name)); 47 | if (s != null) 48 | { 49 | result.Add(new MatchedMember(f, s)); 50 | } 51 | } 52 | return result.ToArray(); 53 | } 54 | 55 | public static Type GetMemberType(MemberInfo mi) 56 | { 57 | if (mi is PropertyInfo) 58 | { 59 | return ((PropertyInfo)mi).PropertyType; 60 | } 61 | else if (mi is FieldInfo) 62 | { 63 | return ((FieldInfo)mi).FieldType; 64 | } 65 | else if (mi is MethodInfo) 66 | { 67 | return ((MethodInfo)mi).ReturnType; 68 | } 69 | return null; 70 | } 71 | 72 | public static bool HasDefaultConstructor(Type type) 73 | { 74 | return type.GetConstructor(new Type[0]) != null; 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /EmitMapper/Utils/ThreadSaveCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace EmitMapper.Utils 7 | { 8 | class ThreadSaveCache 9 | { 10 | Dictionary _cache = new Dictionary(); 11 | 12 | public T Get(string key, Func getter) 13 | { 14 | lock(_cache) 15 | { 16 | object value; 17 | if(!_cache.TryGetValue(key, out value)) 18 | { 19 | value = getter(); 20 | _cache[key] = value; 21 | } 22 | return (T)value; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/Context.cs: -------------------------------------------------------------------------------- 1 | using EmitMapper; 2 | 3 | namespace EmitMapperTests 4 | { 5 | class Context 6 | { 7 | public static ObjectMapperManager objMan = new ObjectMapperManager(); 8 | } 9 | } -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/CustomAssert.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using NUnit.Framework; 5 | 6 | namespace EmitMapperTests 7 | { 8 | class CustomAssert 9 | { 10 | public static void AreEqual(ICollection expected, ICollection actual) 11 | { 12 | Assert.AreEqual(expected.Count, actual.Count); 13 | IEnumerator enumExpected = expected.GetEnumerator(); 14 | IEnumerator enumActual = actual.GetEnumerator(); 15 | while (enumExpected.MoveNext() && enumActual.MoveNext()) 16 | { 17 | Assert.AreEqual(enumExpected.Current, enumActual.Current); 18 | } 19 | } 20 | 21 | public static void AreEqualEnum(IEnumerable expected, IEnumerable actual) 22 | { 23 | Assert.AreEqual(expected.Count(), actual.Count()); 24 | IEnumerator enumExpected = expected.GetEnumerator(); 25 | IEnumerator enumActual = actual.GetEnumerator(); 26 | while (enumExpected.MoveNext() && enumActual.MoveNext()) 27 | { 28 | Assert.AreEqual(enumExpected.Current, enumActual.Current); 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/EmitInvokerTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NUnit.Framework; 6 | using EmitMapper.EmitBuilders; 7 | using EmitMapper; 8 | using System.Diagnostics; 9 | using EmitMapper.EmitInvoker; 10 | using System.Reflection; 11 | 12 | namespace EmitMapperTests 13 | { 14 | [TestFixture] 15 | public class EmitInvokerTest 16 | { 17 | [Test] 18 | public void EmitInvokerTest_TestCall1() 19 | { 20 | int i = 0; 21 | var caller = (DelegateInvokerAction_0)DelegateInvoker.GetDelegateInvoker((Action)(() => i++) ); 22 | caller.CallAction(); 23 | caller.CallAction(); 24 | caller.CallAction(); 25 | Assert.AreEqual(3, i); 26 | 27 | var caller2 = (DelegateInvokerAction_0)DelegateInvoker.GetDelegateInvoker((Action)(() => i += 2)); 28 | caller2.CallAction(); 29 | caller2.CallAction(); 30 | Assert.AreEqual(7, i); 31 | } 32 | 33 | [Test] 34 | public void EmitInvokerTest_TestCall2() 35 | { 36 | var caller = (DelegateInvokerFunc_0)DelegateInvoker.GetDelegateInvoker((Func)(() => 3)); 37 | Assert.AreEqual(3, caller.CallFunc()); 38 | 39 | var caller2 = (DelegateInvokerFunc_2)DelegateInvoker.GetDelegateInvoker((Func)((l, r) => l + r)); 40 | //DynamicAssemblyManager.SaveAssembly(); 41 | Assert.AreEqual(5, caller2.CallFunc(2, 3)); 42 | } 43 | 44 | public int InvokeTest1() 45 | { 46 | return 3; 47 | } 48 | 49 | public static int InvokeTest2(int par) 50 | { 51 | return par; 52 | } 53 | 54 | [Test] 55 | public void EmitInvokerTest_TestCall3() 56 | { 57 | var caller = (MethodInvokerFunc_0)MethodInvoker.GetMethodInvoker(this, GetType().GetMethod("InvokeTest1")); 58 | Assert.AreEqual(3, caller.CallFunc()); 59 | 60 | var caller2 = (MethodInvokerFunc_1)MethodInvoker.GetMethodInvoker(this, GetType().GetMethod("InvokeTest2", BindingFlags.Static | BindingFlags.Public)); 61 | Assert.AreEqual(5, caller2.CallFunc(5)); 62 | 63 | } 64 | 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/EmitMapperTests.NUnit.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | ProjectFiles 4 | 5 | 6 | Program 7 | D:\MyProjects\EmitMapper\trunk\libs\NUnit\nunit.exe 8 | 9 | 10 | Program 11 | d:\MyProjects\EmitMapper\trunk\libs\NUnit\nunit.exe 12 | 13 | -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/EnumTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using EmitMapper; 3 | 4 | namespace EmitMapperTests 5 | { 6 | [TestFixture] 7 | public class EnumTests 8 | { 9 | public enum En1 : byte 10 | { 11 | a = 1, 12 | b = 2, 13 | c = 3 14 | } 15 | 16 | public enum En2 : long 17 | { 18 | a = 1, 19 | b = 2, 20 | c = 3 21 | } 22 | 23 | public enum En3 : int 24 | { 25 | b = 2, 26 | c = 3, 27 | a = 1 28 | } 29 | 30 | public class A 31 | { 32 | public En1 en1 { get; set; } 33 | public En2 en2; 34 | public En3 en3 { get; set; } 35 | public decimal en4; 36 | public string en5; 37 | public En1? en6; 38 | public En3 en7; 39 | public En3? en8; 40 | public En3? en9 = En3.c; 41 | } 42 | 43 | public class B 44 | { 45 | public decimal en1 = 3; 46 | public En1 en2 { get; set; } 47 | public string en3 = "c"; 48 | public En2 en4 = En2.b; 49 | public En3 en5 = En3.a; 50 | public En2 en6 = En2.c; 51 | public En1? en7 =En1.c; 52 | public En1? en8 = En1.c; 53 | public En2? en9 = null; 54 | 55 | public B() 56 | { 57 | en2 = En1.c; 58 | } 59 | } 60 | [Test] 61 | public void EnumTests1() 62 | { 63 | var mapper = Context.objMan.GetMapper(); 64 | //DynamicAssemblyManager.SaveAssembly(); 65 | 66 | var a = mapper.Map(new B()); 67 | Assert.IsTrue(a.en1 == En1.c); 68 | Assert.IsTrue(a.en2 == En2.c); 69 | Assert.IsTrue(a.en3 == En3.c); 70 | Assert.IsTrue(a.en4 == 2); 71 | Assert.IsTrue(a.en6 == En1.c); 72 | Assert.IsTrue(a.en7 == En3.c); 73 | Assert.IsTrue(a.en8 == En3.c); 74 | Assert.IsNull(a.en9); 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/Flattering.cs: -------------------------------------------------------------------------------- 1 | using EmitMapper; 2 | using NUnit.Framework; 3 | using EmitMapper.MappingConfiguration; 4 | using EmitMapper.MappingConfiguration.MappingOperations; 5 | 6 | namespace EmitMapperTests 7 | { 8 | [TestFixture] 9 | public class Flattering 10 | { 11 | public class A 12 | { 13 | public string message; 14 | public string message2; 15 | } 16 | 17 | public class B 18 | { 19 | public class IntB 20 | { 21 | public string message = "hello"; 22 | public string GetMessage2() 23 | { 24 | return "medved"; 25 | } 26 | } 27 | 28 | public IntB intB = new IntB(); 29 | } 30 | 31 | [Test] 32 | public void TestFlattering1() 33 | { 34 | var mapper = ObjectMapperManager.DefaultInstance.GetMapper( 35 | new CustomMapConfig 36 | { 37 | GetMappingOperationFunc = (from, to) => 38 | { 39 | return new[] 40 | { 41 | new ReadWriteSimple 42 | { 43 | Source = new MemberDescriptor(new[] { typeof(B).GetMember("intB")[0], typeof(B.IntB).GetMember("message")[0] }), 44 | Destination = new MemberDescriptor(new[] { typeof(A).GetMember("message")[0] }) 45 | }, 46 | new ReadWriteSimple 47 | { 48 | Source = new MemberDescriptor(new[] { typeof(B).GetMember("intB")[0], typeof(B.IntB).GetMember("GetMessage2")[0] }), 49 | Destination = new MemberDescriptor(new[] { typeof(A).GetMember("message2")[0] }) 50 | } 51 | }; 52 | } 53 | } 54 | ); 55 | 56 | B b = new B(); 57 | A a = mapper.Map(b); 58 | Assert.AreEqual(b.intB.message, a.message); 59 | Assert.AreEqual(b.intB.GetMessage2(), a.message2); 60 | } 61 | } 62 | } -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/IgnoreByAttributes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using NUnit.Framework; 6 | using EmitMapper.MappingConfiguration; 7 | using EmitMapper.MappingConfiguration.MappingOperations; 8 | using EmitMapper; 9 | 10 | namespace EmitMapperTests 11 | { 12 | [TestFixture] 13 | public class IgnoreByAttributes 14 | { 15 | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] 16 | public class MyIgnoreAttribute : Attribute 17 | { 18 | } 19 | 20 | public class IgnoreByAttributesSrc 21 | { 22 | [MyIgnoreAttribute] 23 | public string str1 = "IgnoreByAttributesSrc::str1"; 24 | public string str2 = "IgnoreByAttributesSrc::str2"; 25 | } 26 | 27 | public class IgnoreByAttributesDst 28 | { 29 | public string str1 = "IgnoreByAttributesDst::str1"; 30 | public string str2 = "IgnoreByAttributesDst::str2"; 31 | } 32 | 33 | public class MyConfigurator : DefaultMapConfig 34 | { 35 | public override IMappingOperation[] GetMappingOperations(Type from, Type to) 36 | { 37 | base.IgnoreMembers(GetIgnoreFields(from).Concat(GetIgnoreFields(to)).ToArray()); 38 | return base.GetMappingOperations(from, to); 39 | } 40 | private IEnumerable GetIgnoreFields(Type type) 41 | { 42 | return type 43 | .GetFields() 44 | .Where( 45 | f => f.GetCustomAttributes(typeof(MyIgnoreAttribute), false).Length > 0 46 | ) 47 | .Select(f => f.Name) 48 | .Concat( 49 | type 50 | .GetProperties() 51 | .Where( 52 | p => p.GetCustomAttributes(typeof(MyIgnoreAttribute), false).Length > 0 53 | ) 54 | .Select(p => p.Name) 55 | ); 56 | } 57 | } 58 | 59 | [Test] 60 | public void Test() 61 | { 62 | var mapper = ObjectMapperManager.DefaultInstance.GetMapper(new MyConfigurator()); 63 | var dst = mapper.Map(new IgnoreByAttributesSrc()); 64 | Assert.AreEqual("IgnoreByAttributesDst::str1", dst.str1); 65 | Assert.AreEqual("IgnoreByAttributesSrc::str2", dst.str2); 66 | } 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("EmitMapperTests.SL")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("EmitMapperTests.NUnit")] 13 | [assembly: AssemblyCopyright("Copyright © 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("f1af6251-5dd4-4b42-bcda-ca0f0dc4dddd")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | [assembly: InternalsVisibleTo("EmitMapperAssembly")] -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/TestUtils.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace EmitMapperTests 4 | { 5 | public static class TestUtils 6 | { 7 | public static bool AreEqualArraysUnordered(T[] arr1, T[] arr2) 8 | { 9 | if (arr1 == null || arr2 == null) 10 | { 11 | return arr1 == null && arr2 == null; 12 | } 13 | return arr1.Length == arr2.Length && arr1.All(a1 => arr2.Contains(a1)); 14 | } 15 | 16 | public static bool AreEqualArrays(T[] arr1, T[] arr2) 17 | { 18 | if (arr1 == null || arr2 == null) 19 | { 20 | return arr1 == null && arr2 == null; 21 | } 22 | return arr1.Length == arr2.Length && arr1.Select((a1, i) => arr2[i].Equals(a1)).All(b => b); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/testProject.VisualState.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | [0-1000]D:\MyProjects\EmitMapper\trunk\EmitMapperTests.NUnit\testProject.nunit 4 | [0-1000]D:\MyProjects\EmitMapper\trunk\EmitMapperTests.NUnit\testProject.nunit 5 | false 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /EmitMapperTests.NUnit/testProject.nunit: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Samples/EMConfigurations/CaseIgnoreMapping.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using EmitObjectMapper; 4 | 5 | namespace EOMConfigurations 6 | { 7 | public class CaseIgnoreMapping 8 | { 9 | public static void Configurator(Type from, Type to, MappingSettings settings) 10 | { 11 | settings.MappingItems = 12 | settings.MappingItems. 13 | Where( mi => mi.SourceMember != null). 14 | Join( settings.MappingItems.Where( mi => mi.DestinationMember != null), 15 | mi => mi.SourceMember.Name.ToUpper(), 16 | mi => mi.DestinationMember.Name.ToUpper(), 17 | (omi, imi) => new MappingItem(omi.SourceMember, imi.DestinationMember)). 18 | ToArray(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Samples/EMConfigurations/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("EMConfigurations")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("EMConfigurations")] 13 | [assembly: AssemblyCopyright("Copyright © 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("d37e7e7b-edcf-4a0b-a025-3c9f56c57af9")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Samples/EmitMapper.Mvc.Net/FormCollectionMapConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper.MappingConfiguration.MappingOperations; 6 | using EmitMapper.Utils; 7 | using EmitMapper.MappingConfiguration; 8 | using System.Web.Mvc; 9 | 10 | namespace EmitMapper.Mvc.Net 11 | { 12 | public class FormCollectionMapConfig:IMappingConfigurator 13 | { 14 | public IMappingOperation[] GetMappingOperations(Type from, Type to) 15 | { 16 | var members = ReflectionUtils.GetPublicFieldsAndProperties(to); 17 | return members 18 | .Select( 19 | m => 20 | new DestWriteOperation() 21 | { 22 | Destination = new MemberDescriptor(m), 23 | Getter = 24 | (ValueGetter) 25 | ( 26 | (form, valueProviderObj) => 27 | { 28 | var valueProvider = valueProviderObj as IDictionary; 29 | if (valueProvider == null) 30 | { 31 | valueProvider = ((FormCollection)form).ToValueProvider(); 32 | } 33 | 34 | ValueProviderResult res; 35 | if(valueProvider.TryGetValue(m.Name, out res)) 36 | { 37 | return ValueToWrite.ReturnValue(res.ConvertTo(ReflectionUtils.GetMemberType(m))); 38 | } 39 | else 40 | { 41 | return ValueToWrite.Skip(); 42 | } 43 | } 44 | ) 45 | } 46 | ) 47 | .ToArray(); 48 | } 49 | 50 | public string GetConfigurationName() 51 | { 52 | return null; 53 | } 54 | 55 | public IRootMappingOperation GetRootMappingOperation(Type from, Type to) 56 | { 57 | return null; 58 | } 59 | 60 | public StaticConvertersManager GetStaticConvertersManager() 61 | { 62 | return null; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Samples/EmitMapper.Mvc.Net/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("EmitMapper.Mvc.Net")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("EmitMapper.Mvc.Net")] 13 | [assembly: AssemblyCopyright("Copyright © 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("73d2f5b6-084f-4eb8-8c7b-02b2f2db15b7")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Samples/LightDataAccess/CmdParams.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace LightDataAccess 4 | { 5 | public class CmdParams : Dictionary 6 | { 7 | public CmdParams() 8 | { 9 | } 10 | 11 | public CmdParams(Dictionary init): base(init) 12 | { 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Samples/LightDataAccess/CommandBuilder.Insert.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Data.Common; 3 | using EmitMapper; 4 | using EmitMapper.Mappers; 5 | using LightDataAccess.MappingConfigs; 6 | using EmitMapper.MappingConfiguration.MappingOperations; 7 | 8 | namespace LightDataAccess 9 | { 10 | public static partial class CommandBuilder 11 | { 12 | public static DbCommand BuildInsertCommand( 13 | this DbCommand cmd, 14 | object obj, 15 | string tableName, 16 | DbSettings dbSettings 17 | ) 18 | { 19 | return BuildInsertCommand(cmd, obj, tableName, dbSettings, null, null); 20 | } 21 | 22 | public static DbCommand BuildInsertCommand( 23 | this DbCommand cmd, 24 | object obj, 25 | string tableName, 26 | DbSettings dbSettings, 27 | string[] includeFields, 28 | string[] excludeFields 29 | ) 30 | { 31 | IMappingConfigurator config = new AddDbCommandsMappingConfig( 32 | dbSettings, 33 | includeFields, 34 | excludeFields, 35 | "insertop_inc_" + includeFields.ToCSV("_") + "_exc_" + excludeFields.ToCSV("_") 36 | ); 37 | 38 | var mapper = ObjectMapperManager.DefaultInstance.GetMapperImpl( 39 | obj.GetType(), 40 | typeof(DbCommand), 41 | config 42 | ); 43 | 44 | string[] fields = mapper.StroredObjects.OfType().Select(m => m.Source.MemberInfo.Name).ToArray(); 45 | 46 | var cmdStr = 47 | "INSERT INTO " 48 | + tableName + 49 | "(" 50 | + fields 51 | .Select(f => dbSettings.GetEscapedName(f)) 52 | .ToCSV(",") 53 | + ") VALUES (" 54 | + fields 55 | .Select( f => dbSettings.GetParamName(f)) 56 | .ToCSV(",") 57 | + ")" 58 | ; 59 | cmd.CommandText = cmdStr; 60 | cmd.CommandType = System.Data.CommandType.Text; 61 | 62 | mapper.Map(obj, cmd, null); 63 | return cmd; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Samples/LightDataAccess/ConvertUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace LightDataAccess 7 | { 8 | public static class ConvertUtils 9 | { 10 | public static Guid ToGuid(this string str) 11 | { 12 | if (str == null) 13 | { 14 | return Guid.Empty; 15 | } 16 | return new Guid(str); 17 | } 18 | 19 | public static string ToGuidStr(this string str) 20 | { 21 | if (str == null) 22 | { 23 | return null; 24 | } 25 | return str.ToUpper(); 26 | } 27 | 28 | public static string ToGuidStr(this Guid guid) 29 | { 30 | return guid.ToString().ToUpper(); 31 | } 32 | 33 | public static string ToGuidStr(this Guid? guid) 34 | { 35 | if (guid == null) 36 | { 37 | return null; 38 | } 39 | return guid.Value.ToString().ToUpper(); 40 | } 41 | 42 | public static bool ToBool(this short s) 43 | { 44 | return s != 0; 45 | } 46 | 47 | public static short ToShort(this bool b) 48 | { 49 | return b ? (short)1 : (short)0; 50 | } 51 | 52 | public static bool ToBool(this short? s) 53 | { 54 | return s != 0; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Samples/LightDataAccess/DbSettings.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace LightDataAccess 7 | { 8 | public class DbSettings 9 | { 10 | public string firstNameEscapeSymbol; 11 | public string secondNameEscapeSymbol; 12 | public string paramPrefix; 13 | 14 | public static DbSettings MySQL; 15 | public static DbSettings MSSQL; 16 | 17 | static DbSettings() 18 | { 19 | MySQL = new DbSettings 20 | { 21 | firstNameEscapeSymbol = "`", 22 | secondNameEscapeSymbol = "`", 23 | paramPrefix = "@p_" 24 | }; 25 | 26 | MSSQL = new DbSettings 27 | { 28 | firstNameEscapeSymbol = "[", 29 | secondNameEscapeSymbol = "]", 30 | paramPrefix = "@p_" 31 | }; 32 | 33 | } 34 | 35 | public string GetParamName(string fieldName) 36 | { 37 | return paramPrefix + fieldName; 38 | } 39 | 40 | public string GetEscapedName(string name) 41 | { 42 | return firstNameEscapeSymbol + name + secondNameEscapeSymbol; 43 | } 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Samples/LightDataAccess/MappingConfigs/AddDbCommandsMappingConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapper; 6 | using EmitMapper.Utils; 7 | using EmitMapper.MappingConfiguration.MappingOperations; 8 | using EmitMapper.MappingConfiguration; 9 | using System.Data.Common; 10 | 11 | namespace LightDataAccess.MappingConfigs 12 | { 13 | class AddDbCommandsMappingConfig : IMappingConfigurator 14 | { 15 | DbSettings _dbSettings; 16 | IEnumerable _includeFields; 17 | IEnumerable _excludeFields; 18 | string _configName; 19 | 20 | public AddDbCommandsMappingConfig( 21 | DbSettings dbSettings, 22 | IEnumerable includeFields, 23 | IEnumerable excludeFields, 24 | string configName) 25 | { 26 | _dbSettings = dbSettings; 27 | _includeFields = includeFields; 28 | _excludeFields = excludeFields; 29 | _configName = configName; 30 | 31 | if (_includeFields != null) 32 | { 33 | _includeFields = _includeFields.Select(f => f.ToUpper()); 34 | } 35 | 36 | if (_excludeFields != null) 37 | { 38 | _excludeFields = _excludeFields.Select(f => f.ToUpper()); 39 | } 40 | 41 | } 42 | 43 | 44 | #region IMappingConfigurator Members 45 | public IRootMappingOperation GetRootMappingOperation(Type from, Type to) 46 | { 47 | return null; 48 | } 49 | 50 | public IMappingOperation[] GetMappingOperations(Type from, Type to) 51 | { 52 | var members = ReflectionUtils.GetPublicFieldsAndProperties(from); 53 | if (_includeFields != null) 54 | { 55 | members = members 56 | .Where( m => _includeFields.Contains(m.Name.ToUpper()) ) 57 | .ToArray(); 58 | } 59 | 60 | if (_excludeFields != null) 61 | { 62 | members = members 63 | .Where( m => !_excludeFields.Contains(m.Name.ToUpper()) ) 64 | .ToArray(); 65 | } 66 | 67 | return members 68 | .Select( 69 | m => new SrcReadOperation 70 | { 71 | Source = new MemberDescriptor(new[] { m }), 72 | Setter = (obj, v, s) => ((DbCommand)obj).AddParam(_dbSettings.paramPrefix + m.Name, v) 73 | } 74 | ) 75 | .ToArray(); 76 | } 77 | 78 | public string GetConfigurationName() 79 | { 80 | return _configName; 81 | } 82 | 83 | 84 | #endregion 85 | 86 | public StaticConvertersManager GetStaticConvertersManager() 87 | { 88 | return null; 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /Samples/LightDataAccess/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("LightDataAccess")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("LightDataAccess")] 13 | [assembly: AssemblyCopyright("Copyright © 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("560f39cc-844b-45cd-9b30-9b88a8517fb4")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | 38 | [assembly: InternalsVisibleTo("EmitMapperAssembly")] -------------------------------------------------------------------------------- /Samples/LightDataAccess/SelectConstraints.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace LightDataAccess 7 | { 8 | public class FilterConstraints 9 | { 10 | List _Constraints { get; set; } 11 | public CmdParams Params { get; set; } 12 | 13 | public FilterConstraints() 14 | { 15 | _Constraints = new List(); 16 | Params = new CmdParams(); 17 | } 18 | 19 | public string BuildWhere() 20 | { 21 | return (_Constraints.Count > 0 ? "WHERE " : "") + _Constraints.Select( c => "(" + c + ")").ToCSV(" AND "); 22 | } 23 | 24 | public void Add(string constraint) 25 | { 26 | Add(constraint, null); 27 | } 28 | 29 | public void Add(string constraint, CmdParams Params) 30 | { 31 | _Constraints.Add(constraint); 32 | if(Params != null) 33 | { 34 | foreach( var p in Params ) 35 | { 36 | this.Params.Add(p.Key, p.Value); 37 | } 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Samples/LightDataAccess/ThreadConnection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Data.Common; 6 | 7 | namespace LightDataAccess 8 | { 9 | public class ThreadConnection: IDisposable 10 | { 11 | [ThreadStatic] 12 | private static DbConnection connection; 13 | 14 | [ThreadStatic] 15 | private static int entriesCount; 16 | 17 | public ThreadConnection(Func connectionCreator) 18 | { 19 | if (connection == null || connection.State == System.Data.ConnectionState.Broken) 20 | { 21 | connection = connectionCreator(); 22 | } 23 | entriesCount++; 24 | } 25 | 26 | public DbConnection Connection 27 | { 28 | get 29 | { 30 | if (connection.State == System.Data.ConnectionState.Closed) 31 | { 32 | connection.Open(); 33 | } 34 | return connection; 35 | } 36 | } 37 | 38 | #region IDisposable Members 39 | 40 | public void Dispose() 41 | { 42 | if (entriesCount <= 1) 43 | { 44 | if (connection != null) 45 | { 46 | connection.Close(); 47 | } 48 | connection = null; 49 | entriesCount = 0; 50 | } 51 | else 52 | { 53 | entriesCount--; 54 | } 55 | } 56 | 57 | #endregion 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /SamplesTests/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /SamplesTests/Flattering.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using EMConfigurations; 4 | 5 | namespace EmitMapper.Samples.SamplesTests 6 | { 7 | [TestClass] 8 | public class Flattering 9 | { 10 | public class ModelObject 11 | { 12 | public DateTime BaseDate { get; set; } 13 | public ModelSubObject Sub { get; set; } 14 | public ModelSubObject Sub2 { get; set; } 15 | public ModelSubObject SubWithExtraName { get; set; } 16 | } 17 | 18 | public class ModelSubObject 19 | { 20 | public string ProperName { get; set; } 21 | public ModelSubSubObject SubSub { get; set; } 22 | } 23 | 24 | public class ModelSubSubObject 25 | { 26 | public string IAmACoolProperty { get; set; } 27 | } 28 | 29 | public class ModelDto 30 | { 31 | public DateTime BaseDate { get; set; } 32 | public string SubProperName { get; set; } 33 | public string Sub2ProperName { get; set; } 34 | public string SubWithExtraNameProperName { get; set; } 35 | public string SubSubSubIAmACoolProperty { get; set; } 36 | } 37 | 38 | [TestMethod] 39 | public void TestFlattering() 40 | { 41 | var source = new ModelObject 42 | { 43 | BaseDate = DateTime.Now, 44 | Sub = new ModelSubObject 45 | { 46 | ProperName = "Some name", 47 | SubSub = new ModelSubSubObject 48 | { 49 | IAmACoolProperty = "Cool daddy-o" 50 | } 51 | }, 52 | Sub2 = new ModelSubObject 53 | { 54 | ProperName = "Sub 2 name" 55 | }, 56 | SubWithExtraName = new ModelSubObject 57 | { 58 | ProperName = "Some other name" 59 | }, 60 | }; 61 | 62 | var mapper = ObjectMapperManager.DefaultInstance.GetMapper( 63 | new FlatteringConfig() 64 | ); 65 | 66 | var b = mapper.Map(source); 67 | } 68 | 69 | } 70 | } -------------------------------------------------------------------------------- /SamplesTests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("SamplesTests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("SamplesTests")] 13 | [assembly: AssemblyCopyright("Copyright © 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM componenets. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("d78518d9-f03f-4d07-9754-4b381a8e2738")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Revision and Build Numbers 33 | // by using the '*' as shown below: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | 37 | [assembly: InternalsVisibleTo("EmitMapperAssembly")] -------------------------------------------------------------------------------- /SamplesTests/TestMappingToDataRow.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Microsoft.VisualStudio.TestTools.UnitTesting; 6 | using EmitMapper.MappingConfiguration; 7 | using EmitMapper.MappingConfiguration.MappingOperations; 8 | using EmitMapper.Utils; 9 | using System.Data; 10 | 11 | namespace EmitMapper.Samples.SamplesTests 12 | { 13 | [TestClass] 14 | public class TestMappingToDataRow 15 | { 16 | public class Map2DataRowConfig : MapConfigBase 17 | { 18 | public override IMappingOperation[] GetMappingOperations(Type from, Type to) 19 | { 20 | var objectMembers = ReflectionUtils.GetPublicFieldsAndProperties(from); 21 | return base.FilterOperations( 22 | from, 23 | to, 24 | objectMembers.Select( 25 | m => (IMappingOperation)new SrcReadOperation 26 | { 27 | Source = new MemberDescriptor(m), 28 | Setter = (obj, value, state) => 29 | { 30 | ((DataRow)obj)[m.Name] = value ?? DBNull.Value; 31 | } 32 | } 33 | ) 34 | ).ToArray(); 35 | } 36 | } 37 | // Using: 38 | 39 | // Test data object 40 | public class TestDTO 41 | { 42 | public string field1 = "field1"; 43 | public int field2 = 10; 44 | public bool field3 = true; 45 | } 46 | 47 | [TestMethod] 48 | public void MappingToDataRow_test() 49 | { 50 | // this is the mapper 51 | var mapper = ObjectMapperManager.DefaultInstance.GetMapper(new Map2DataRowConfig()); 52 | 53 | // initialization of test DTO object 54 | TestDTO testDataObject = new TestDTO 55 | { 56 | field1 = "field1", 57 | field2 = 10, 58 | field3 = true 59 | }; 60 | 61 | // Initializing of test table. Usual this table is read from database. 62 | DataTable dt = new DataTable(); 63 | dt.Columns.Add("field1", typeof(string)); 64 | dt.Columns.Add("field2", typeof(int)); 65 | dt.Columns.Add("field3", typeof(bool)); 66 | dt.Rows.Add(); 67 | DataRow dr = dt.Rows[0]; 68 | 69 | // Mapping test object to datarow 70 | mapper.Map(testDataObject, dr); 71 | 72 | // Check if object is correctly mapped 73 | Assert.AreEqual("field1", dr["field1"]); 74 | Assert.AreEqual(10, dr["field2"]); 75 | Assert.AreEqual(true, dr["field3"]); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /SamplesTests/TestObjectsTracker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using EmitMapperTests; 6 | using Microsoft.VisualStudio.TestTools.UnitTesting; 7 | 8 | namespace LightDataAccess 9 | { 10 | [TestClass] 11 | public class TestObjectsTracker 12 | { 13 | public class A 14 | { 15 | public string f1 = "f1"; 16 | public int f2 = 2; 17 | public bool f3 = true; 18 | } 19 | 20 | [TestMethod] 21 | public void TestObjectsTracking() 22 | { 23 | var tracker = new ObjectsChangeTracker(); 24 | var a = new A(); 25 | tracker.RegisterObject(a); 26 | a.f2 = 3; 27 | var changes = tracker.GetChanges(a); 28 | Assert.IsTrue( changes[0].name == "f2"); 29 | tracker.RegisterObject(a); 30 | changes = tracker.GetChanges(a); 31 | Assert.IsTrue(changes.Length == 0); 32 | 33 | a.f1 = "new"; 34 | a.f2 = 13; 35 | a.f3 = false; 36 | for (int i = 0; i < 10; ++i) 37 | { 38 | tracker.GetChanges(a); 39 | } 40 | 41 | changes = tracker.GetChanges(a); 42 | Assert.IsTrue(TestUtils.AreEqualArraysUnordered(new[] { "f1", "f2", "f3" }, changes.Select(c => c.name).ToArray())); 43 | 44 | changes = tracker.GetChanges(new A()); 45 | Assert.IsNull(changes); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Silverlight/EmitMapper.SL/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("EmitMapper.SL")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("EmitMapper.SL")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | [assembly: InternalsVisibleTo("EmitMapperAssembly.SL")] 18 | 19 | // Setting ComVisible to false makes the types in this assembly not visible 20 | // to COM components. If you need to access a type in this assembly from 21 | // COM, set the ComVisible attribute to true on that type. 22 | [assembly: ComVisible(false)] 23 | 24 | // The following GUID is for the ID of the typelib if this project is exposed to COM 25 | [assembly: Guid("9e12e682-22bf-4c49-9a5f-23037f3a223e")] 26 | 27 | // Version information for an assembly consists of the following four values: 28 | // 29 | // Major Version 30 | // Minor Version 31 | // Build Number 32 | // Revision 33 | // 34 | // You can specify all the values or you can default the Revision and Build Numbers 35 | // by using the '*' as shown below: 36 | [assembly: AssemblyVersion("1.0.0.0")] 37 | [assembly: AssemblyFileVersion("1.0.0.0")] 38 | -------------------------------------------------------------------------------- /Silverlight/EmitMapperTests.SL/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("EmitMapperTests.SL")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("EmitMapperTests.SL")] 13 | [assembly: AssemblyCopyright("Copyright © 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("29cbcc92-32f6-4ad4-bf3a-7554e567964b")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | using System; 8 | using System.Reflection; 9 | 10 | [assembly: CLSCompliant(true)] 11 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/AssertionException.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | using System.Runtime.Serialization; 11 | 12 | /// 13 | /// Thrown when an assertion failed. 14 | /// 15 | /// 16 | [Serializable] 17 | public class AssertionException : System.Exception 18 | { 19 | /// The error message that explains 20 | /// the reason for the exception 21 | public AssertionException (string message) : base(message) 22 | {} 23 | 24 | /// The error message that explains 25 | /// the reason for the exception 26 | /// The exception that caused the 27 | /// current exception 28 | public AssertionException(string message, Exception inner) : 29 | base(message, inner) 30 | {} 31 | 32 | /// 33 | /// Serialization Constructor 34 | /// 35 | protected AssertionException(SerializationInfo info, 36 | StreamingContext context) : base(info,context) 37 | {} 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/CategoryAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// Attribute used to apply a category to a test 13 | /// 14 | [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Assembly, AllowMultiple=true)] 15 | public class CategoryAttribute : Attribute 16 | { 17 | /// 18 | /// The name of the category 19 | /// 20 | protected string categoryName; 21 | 22 | /// 23 | /// Construct attribute for a given category 24 | /// 25 | /// The name of the category 26 | public CategoryAttribute(string name) 27 | { 28 | this.categoryName = name; 29 | } 30 | 31 | /// 32 | /// Protected constructor uses the Type name as the name 33 | /// of the category. 34 | /// 35 | protected CategoryAttribute() 36 | { 37 | this.categoryName = this.GetType().Name; 38 | if ( categoryName.EndsWith( "Attribute" ) ) 39 | categoryName = categoryName.Substring( 0, categoryName.Length - 9 ); 40 | } 41 | 42 | /// 43 | /// The name of the category 44 | /// 45 | public string Name 46 | { 47 | get { return categoryName; } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/CommonAssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | using System.Reflection; 8 | 9 | // 10 | // Common Information about all NUnit assemblies is controlled through the following 11 | // set of attributes. Change these source values to modify the information 12 | // associated with an assembly. 13 | // 14 | // NOTE: This file is only used in the Visual Studio builds. For the NAnt builds, 15 | // an alternate file is generated and used. 16 | // 17 | [assembly: AssemblyCompany("NUnit.org")] 18 | [assembly: AssemblyProduct("NUnit")] 19 | [assembly: AssemblyCopyright("Copyright (C) 2002-2009 Charlie Poole.\r\nCopyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.\r\nCopyright (C) 2000-2002 Philip Craig.\r\nAll Rights Reserved.")] 20 | [assembly: AssemblyTrademark("NUnit is a trademark of NUnit.org")] 21 | 22 | // 23 | // Version information for an assembly consists of the following four values: 24 | // 25 | // Major Version 26 | // Minor Version 27 | // Build Number 28 | // Revision 29 | // 30 | // You can specify all the values or you can default the Revision and Build Numbers 31 | // by using the '*' as shown below: 32 | 33 | [assembly: AssemblyVersion("2.5.3")] 34 | [assembly: AssemblyInformationalVersion("2.5.3")] 35 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/Constraints/EmptyConstraint.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | using System.Collections; 9 | 10 | namespace NUnit.Framework.Constraints 11 | { 12 | /// 13 | /// EmptyConstraint tests a whether a string or collection is empty, 14 | /// postponing the decision about which test is applied until the 15 | /// type of the actual argument is known. 16 | /// 17 | public class EmptyConstraint : Constraint 18 | { 19 | private Constraint RealConstraint 20 | { 21 | get 22 | { 23 | if ( actual is string ) 24 | return new EmptyStringConstraint(); 25 | else 26 | return new EmptyCollectionConstraint(); 27 | } 28 | } 29 | 30 | /// 31 | /// Test whether the constraint is satisfied by a given value 32 | /// 33 | /// The value to be tested 34 | /// True for success, false for failure 35 | public override bool Matches(object actual) 36 | { 37 | this.actual = actual; 38 | 39 | return this.RealConstraint.Matches( actual ); 40 | } 41 | 42 | /// 43 | /// Write the constraint description to a MessageWriter 44 | /// 45 | /// The writer on which the description is displayed 46 | public override void WriteDescriptionTo(MessageWriter writer) 47 | { 48 | this.RealConstraint.WriteDescriptionTo( writer ); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/Constraints/IResolveConstraint.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2008, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework.Constraints 8 | { 9 | /// 10 | /// The IConstraintExpression interface is implemented by all 11 | /// complete and resolvable constraints and expressions. 12 | /// 13 | public interface IResolveConstraint 14 | { 15 | /// 16 | /// Return the top-level constraint for this expression 17 | /// 18 | /// 19 | Constraint Resolve(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/Constraints/NUnitComparer.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2009, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | using System.IO; 9 | using System.Reflection; 10 | using System.Collections; 11 | #if NET_2_0 12 | using System.Collections.Generic; 13 | #endif 14 | 15 | namespace NUnit.Framework.Constraints 16 | { 17 | /// 18 | /// NUnitComparer encapsulates NUnit's default behavior 19 | /// in comparing two objects. 20 | /// 21 | public class NUnitComparer : IComparer 22 | { 23 | /// 24 | /// Returns the default NUnitComparer. 25 | /// 26 | public static NUnitComparer Default 27 | { 28 | get { return new NUnitComparer(); } 29 | } 30 | 31 | /// 32 | /// Compares two objects 33 | /// 34 | /// 35 | /// 36 | /// 37 | public int Compare(object x, object y) 38 | { 39 | if (x == null) 40 | return y == null ? 0 : -1; 41 | else if (y == null) 42 | return +1; 43 | 44 | if (Numerics.IsNumericType(x) && Numerics.IsNumericType(y)) 45 | return Numerics.Compare(x, y); 46 | 47 | if (x is IComparable) 48 | return ((IComparable)x).CompareTo(y); 49 | 50 | if (y is IComparable) 51 | return -((IComparable)y).CompareTo(x); 52 | 53 | Type xType = x.GetType(); 54 | Type yType = y.GetType(); 55 | 56 | MethodInfo method = xType.GetMethod("CompareTo", new Type[] { yType }); 57 | if (method != null) 58 | return (int)method.Invoke(x, new object[] { y }); 59 | 60 | method = yType.GetMethod("CompareTo", new Type[] { xType }); 61 | if (method != null) 62 | return -(int)method.Invoke(y, new object[] { x }); 63 | 64 | throw new ArgumentException("Neither value implements IComparable or IComparable"); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/Constraints/PredicateConstraint.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2009, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | #if NET_2_0 8 | using System; 9 | using System.Collections.Generic; 10 | 11 | namespace NUnit.Framework.Constraints 12 | { 13 | /// 14 | /// Predicate constraint wraps a Predicate in a constraint, 15 | /// returning success if the predicate is true. 16 | /// 17 | public class PredicateConstraint : Constraint 18 | { 19 | Predicate predicate; 20 | 21 | /// 22 | /// Construct a PredicateConstraint from a predicate 23 | /// 24 | public PredicateConstraint(Predicate predicate) 25 | { 26 | this.predicate = predicate; 27 | } 28 | 29 | /// 30 | /// Determines whether the predicate succeeds when applied 31 | /// to the actual value. 32 | /// 33 | public override bool Matches(object actual) 34 | { 35 | this.actual = actual; 36 | 37 | if (!(actual is T)) 38 | throw new ArgumentException("The actual value is not of type " + typeof(T).Name, "actual"); 39 | 40 | return predicate((T)actual); 41 | } 42 | 43 | /// 44 | /// Writes the description to a MessageWriter 45 | /// 46 | public override void WriteDescriptionTo(MessageWriter writer) 47 | { 48 | writer.WritePredicate("value matching"); 49 | writer.Write(predicate.Method.Name.StartsWith("<") 50 | ? "lambda expression" 51 | : predicate.Method.Name); 52 | } 53 | } 54 | } 55 | #endif 56 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/Constraints/ResolvableConstraintExpression.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2008, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework.Constraints 10 | { 11 | /// 12 | /// ResolvableConstraintExpression is used to represent a compound 13 | /// constraint being constructed at a point where the last operator 14 | /// may either terminate the expression or may have additional 15 | /// qualifying constraints added to it. 16 | /// 17 | /// It is used, for example, for a Property element or for 18 | /// an Exception element, either of which may be optionally 19 | /// followed by constraints that apply to the property or 20 | /// exception. 21 | /// 22 | public class ResolvableConstraintExpression : ConstraintExpression, IResolveConstraint 23 | { 24 | /// 25 | /// Create a new instance of ResolvableConstraintExpression 26 | /// 27 | public ResolvableConstraintExpression() { } 28 | 29 | /// 30 | /// Create a new instance of ResolvableConstraintExpression, 31 | /// passing in a pre-populated ConstraintBuilder. 32 | /// 33 | public ResolvableConstraintExpression(ConstraintBuilder builder) 34 | : base(builder) { } 35 | 36 | /// 37 | /// Appends an And Operator to the expression 38 | /// 39 | public ConstraintExpression And 40 | { 41 | get { return this.Append(new AndOperator()); } 42 | } 43 | 44 | /// 45 | /// Appends an Or operator to the expression. 46 | /// 47 | public ConstraintExpression Or 48 | { 49 | get { return this.Append(new OrOperator()); } 50 | } 51 | 52 | #region IResolveConstraint Members 53 | /// 54 | /// Resolve the current expression to a Constraint 55 | /// 56 | Constraint IResolveConstraint.Resolve() 57 | { 58 | return builder.Resolve(); 59 | } 60 | #endregion 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/Constraints/SameAsConstraint.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework.Constraints 10 | { 11 | /// 12 | /// SameAsConstraint tests whether an object is identical to 13 | /// the object passed to its constructor 14 | /// 15 | public class SameAsConstraint : Constraint 16 | { 17 | private object expected; 18 | 19 | /// 20 | /// Initializes a new instance of the class. 21 | /// 22 | /// The expected object. 23 | public SameAsConstraint(object expected) : base(expected) 24 | { 25 | this.expected = expected; 26 | } 27 | 28 | /// 29 | /// Test whether the constraint is satisfied by a given value 30 | /// 31 | /// The value to be tested 32 | /// True for success, false for failure 33 | public override bool Matches(object actual) 34 | { 35 | this.actual = actual; 36 | 37 | #if NETCF_1_0 38 | // TODO: THis makes it compile, now make it work. 39 | return expected.Equals(actual); 40 | #else 41 | return Object.ReferenceEquals(expected, actual); 42 | #endif 43 | } 44 | 45 | /// 46 | /// Write the constraint description to a MessageWriter 47 | /// 48 | /// The writer on which the description is displayed 49 | public override void WriteDescriptionTo(MessageWriter writer) 50 | { 51 | writer.WritePredicate("same as"); 52 | writer.WriteExpectedValue(expected); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/Contains.cs: -------------------------------------------------------------------------------- 1 | // *********************************************************************** 2 | // Copyright (c) 2009 Charlie Poole 3 | // 4 | // Permission is hereby granted, free of charge, to any person obtaining 5 | // a copy of this software and associated documentation files (the 6 | // "Software"), to deal in the Software without restriction, including 7 | // without limitation the rights to use, copy, modify, merge, publish, 8 | // distribute, sublicense, and/or sell copies of the Software, and to 9 | // permit persons to whom the Software is furnished to do so, subject to 10 | // the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be 13 | // included in all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | // *********************************************************************** 23 | 24 | using System; 25 | using System.Collections; 26 | using NUnit.Framework.Constraints; 27 | 28 | namespace NUnit.Framework 29 | { 30 | /// 31 | /// Static helper class used in the constraint-based syntax 32 | /// 33 | public class Contains 34 | { 35 | /// 36 | /// Creates a new SubstringConstraint 37 | /// 38 | /// The value of the substring 39 | /// A SubstringConstraint 40 | public static Constraint Substring(string substring) 41 | { 42 | return new SubstringConstraint(substring); 43 | } 44 | 45 | /// 46 | /// Creates a new CollectionContainsConstraint. 47 | /// 48 | /// The item that should be found. 49 | /// A new CollectionContainsConstraint 50 | public static Constraint Item(object item) 51 | { 52 | return new CollectionContainsConstraint(item); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/DatapointAttributes.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2008, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// Used to mark a field for use as a datapoint when executing a theory 13 | /// within the same fixture that requires an argument of the field's Type. 14 | /// 15 | [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] 16 | public class DatapointAttribute : Attribute 17 | { 18 | } 19 | 20 | /// 21 | /// Used to mark an array as containing a set of datapoints to be used 22 | /// executing a theory within the same fixture that requires an argument 23 | /// of the Type of the array elements. 24 | /// 25 | [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] 26 | public class DatapointsAttribute : Attribute 27 | { 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/DescriptionAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// Attribute used to provide descriptive text about a 13 | /// test case or fixture. 14 | /// 15 | [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Assembly, AllowMultiple=false)] 16 | public class DescriptionAttribute : Attribute 17 | { 18 | string description; 19 | 20 | /// 21 | /// Construct the attribute 22 | /// 23 | /// Text describing the test 24 | public DescriptionAttribute(string description) 25 | { 26 | this.description=description; 27 | } 28 | 29 | /// 30 | /// Gets the test description 31 | /// 32 | public string Description 33 | { 34 | get { return description; } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/ExplicitAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// ExplicitAttribute marks a test or test fixture so that it will 13 | /// only be run if explicitly executed from the gui or command line 14 | /// or if it is included by use of a filter. The test will not be 15 | /// run simply because an enclosing suite is run. 16 | /// 17 | [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Assembly, AllowMultiple=false)] 18 | public class ExplicitAttribute : Attribute 19 | { 20 | private string reason; 21 | 22 | /// 23 | /// Default constructor 24 | /// 25 | public ExplicitAttribute() 26 | { 27 | this.reason = ""; 28 | } 29 | 30 | /// 31 | /// Constructor with a reason 32 | /// 33 | /// The reason test is marked explicit 34 | public ExplicitAttribute(string reason) 35 | { 36 | this.reason = reason; 37 | } 38 | 39 | /// 40 | /// The reason test is marked explicit 41 | /// 42 | public string Reason 43 | { 44 | get { return reason; } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/GlobalSettings.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2008, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// GlobalSettings is a place for setting default values used 13 | /// by the framework in performing asserts. 14 | /// 15 | public class GlobalSettings 16 | { 17 | /// 18 | /// Default tolerance for floating point equality 19 | /// 20 | public static double DefaultFloatingPointTolerance = 0.0d; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/IExpectException.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// Interface implemented by a user fixture in order to 13 | /// validate any expected exceptions. It is only called 14 | /// for test methods marked with the ExpectedException 15 | /// attribute. 16 | /// 17 | public interface IExpectException 18 | { 19 | /// 20 | /// Method to handle an expected exception 21 | /// 22 | /// The exception to be handled 23 | void HandleException(Exception ex); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/ITestCaseData.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2008, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// The ITestCaseData interface is implemented by a class 13 | /// that is able to return complete testcases for use by 14 | /// a parameterized test method. 15 | /// 16 | /// NOTE: This interface is used in both the framework 17 | /// and the core, even though that results in two different 18 | /// types. However, sharing the source code guarantees that 19 | /// the various implementations will be compatible and that 20 | /// the core is able to reflect successfully over the 21 | /// framework implementations of ITestCaseData. 22 | /// 23 | public interface ITestCaseData 24 | { 25 | /// 26 | /// Gets the argument list to be provided to the test 27 | /// 28 | object[] Arguments { get; } 29 | 30 | /// 31 | /// Gets the expected result 32 | /// 33 | object Result { get; } 34 | 35 | /// 36 | /// Gets the expected exception Type 37 | /// 38 | Type ExpectedException { get; } 39 | 40 | /// 41 | /// Gets the FullName of the expected exception 42 | /// 43 | string ExpectedExceptionName { get; } 44 | 45 | /// 46 | /// Gets the name to be used for the test 47 | /// 48 | string TestName { get; } 49 | 50 | /// 51 | /// Gets the description of the test 52 | /// 53 | string Description { get; } 54 | 55 | /// 56 | /// Gets a value indicating whether this is ignored. 57 | /// 58 | /// true if ignored; otherwise, false. 59 | bool Ignored { get; } 60 | 61 | /// 62 | /// Gets the ignore reason. 63 | /// 64 | /// The ignore reason. 65 | string IgnoreReason { get; } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/IgnoreAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | 11 | /// 12 | /// Attribute used to mark a test that is to be ignored. 13 | /// Ignored tests result in a warning message when the 14 | /// tests are run. 15 | /// 16 | [AttributeUsage(AttributeTargets.Method|AttributeTargets.Class|AttributeTargets.Assembly, AllowMultiple=false)] 17 | public class IgnoreAttribute : Attribute 18 | { 19 | private string reason; 20 | 21 | /// 22 | /// Constructs the attribute without giving a reason 23 | /// for ignoring the test. 24 | /// 25 | public IgnoreAttribute() 26 | { 27 | this.reason = ""; 28 | } 29 | 30 | /// 31 | /// Constructs the attribute giving a reason for ignoring the test 32 | /// 33 | /// The reason for ignoring the test 34 | public IgnoreAttribute(string reason) 35 | { 36 | this.reason = reason; 37 | } 38 | 39 | /// 40 | /// The reason for ignoring a test 41 | /// 42 | public string Reason 43 | { 44 | get { return reason; } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/IgnoreException.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | using System.Runtime.Serialization; 11 | 12 | /// 13 | /// Thrown when an assertion failed. 14 | /// 15 | [Serializable] 16 | public class IgnoreException : System.Exception 17 | { 18 | /// 19 | public IgnoreException (string message) : base(message) 20 | {} 21 | 22 | /// The error message that explains 23 | /// the reason for the exception 24 | /// The exception that caused the 25 | /// current exception 26 | public IgnoreException(string message, Exception inner) : 27 | base(message, inner) 28 | {} 29 | 30 | /// 31 | /// Serialization Constructor 32 | /// 33 | protected IgnoreException(SerializationInfo info, 34 | StreamingContext context) : base(info,context) 35 | {} 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/InconclusiveException.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | using System.Runtime.Serialization; 11 | 12 | /// 13 | /// Thrown when a test executes inconclusively. 14 | /// 15 | /// 16 | [Serializable] 17 | public class InconclusiveException : System.Exception 18 | { 19 | /// The error message that explains 20 | /// the reason for the exception 21 | public InconclusiveException(string message) 22 | : base(message) 23 | { } 24 | 25 | /// The error message that explains 26 | /// the reason for the exception 27 | /// The exception that caused the 28 | /// current exception 29 | public InconclusiveException(string message, Exception inner) 30 | : 31 | base(message, inner) 32 | { } 33 | 34 | /// 35 | /// Serialization Constructor 36 | /// 37 | protected InconclusiveException(SerializationInfo info, 38 | StreamingContext context) 39 | : base(info, context) 40 | { } 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/Iz.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | using System; 7 | 8 | namespace NUnit.Framework 9 | { 10 | /// 11 | /// The Iz class is a synonym for Is intended for use in VB, 12 | /// which regards Is as a keyword. 13 | /// 14 | public class Iz : Is 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/JoinTypeAttributes.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2008, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// Marks a test to use a combinatorial join of any argument data 13 | /// provided. NUnit will create a test case for every combination of 14 | /// the arguments provided. This can result in a large number of test 15 | /// cases and so should be used judiciously. This is the default join 16 | /// type, so the attribute need not be used except as documentation. 17 | /// 18 | [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] 19 | public class CombinatorialAttribute : PropertyAttribute 20 | { 21 | /// 22 | /// Default constructor 23 | /// 24 | public CombinatorialAttribute() : base("_JOINTYPE", "Combinatorial") { } 25 | } 26 | 27 | /// 28 | /// Marks a test to use pairwise join of any argument data provided. 29 | /// NUnit will attempt too excercise every pair of argument values at 30 | /// least once, using as small a number of test cases as it can. With 31 | /// only two arguments, this is the same as a combinatorial join. 32 | /// 33 | [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] 34 | public class PairwiseAttribute : PropertyAttribute 35 | { 36 | /// 37 | /// Default constructor 38 | /// 39 | public PairwiseAttribute() : base("_JOINTYPE", "Pairwise") { } 40 | } 41 | 42 | /// 43 | /// Marks a test to use a sequential join of any argument data 44 | /// provided. NUnit will use arguements for each parameter in 45 | /// sequence, generating test cases up to the largest number 46 | /// of argument values provided and using null for any arguments 47 | /// for which it runs out of values. Normally, this should be 48 | /// used with the same number of arguments for each parameter. 49 | /// 50 | [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] 51 | public class SequentialAttribute : PropertyAttribute 52 | { 53 | /// 54 | /// Default constructor 55 | /// 56 | public SequentialAttribute() : base("_JOINTYPE", "Sequential") { } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/List.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | using System.Collections; 9 | using NUnit.Framework.Constraints; 10 | 11 | namespace NUnit.Framework 12 | { 13 | /// 14 | /// The List class is a helper class with properties and methods 15 | /// that supply a number of constraints used with lists and collections. 16 | /// 17 | public class List 18 | { 19 | /// 20 | /// List.Map returns a ListMapper, which can be used to map 21 | /// the original collection to another collection. 22 | /// 23 | /// 24 | /// 25 | public static ListMapper Map( ICollection actual ) 26 | { 27 | return new ListMapper( actual ); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/ListMapper.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2008, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | using System.Collections; 9 | using System.Reflection; 10 | using System.Collections.Generic; 11 | 12 | namespace NUnit.Framework 13 | { 14 | /// 15 | /// ListMapper is used to transform a collection used as an actual argument 16 | /// producing another collection to be used in the assertion. 17 | /// 18 | public class ListMapper 19 | { 20 | ICollection original; 21 | 22 | /// 23 | /// Construct a ListMapper based on a collection 24 | /// 25 | /// The collection to be transformed 26 | public ListMapper( ICollection original ) 27 | { 28 | this.original = original; 29 | } 30 | 31 | /// 32 | /// Produces a collection containing all the values of a property 33 | /// 34 | /// The collection of property values 35 | /// 36 | public ICollection Property( string name ) 37 | { 38 | List propList = new List(); 39 | foreach( object item in original ) 40 | { 41 | PropertyInfo property = item.GetType().GetProperty( name, 42 | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ); 43 | if ( property == null ) 44 | throw new ArgumentException( string.Format( 45 | "{0} does not have a {1} property", item, name ) ); 46 | 47 | propList.Add( property.GetValue( item, null ) ); 48 | } 49 | 50 | return propList; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/MaxTimeAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | using System; 7 | 8 | namespace NUnit.Framework 9 | { 10 | /// 11 | /// Summary description for MaxTimeAttribute. 12 | /// 13 | [AttributeUsage( AttributeTargets.Method, AllowMultiple=false, Inherited=false )] 14 | public sealed class MaxTimeAttribute : PropertyAttribute 15 | { 16 | /// 17 | /// Construct a MaxTimeAttribute, given a time in milliseconds. 18 | /// 19 | /// The maximum elapsed time in milliseconds 20 | public MaxTimeAttribute( int milliseconds ) 21 | : base( milliseconds ) { } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/RepeatAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// RepeatAttribute may be applied to test case in order 13 | /// to run it multiple times. 14 | /// 15 | [AttributeUsage(AttributeTargets.Method, AllowMultiple=false)] 16 | public class RepeatAttribute : PropertyAttribute 17 | { 18 | /// 19 | /// Construct a RepeatAttribute 20 | /// 21 | /// The number of times to run the test 22 | public RepeatAttribute(int count) : base(count) { } 23 | 24 | //private int count; 25 | 26 | ///// 27 | ///// Construct a RepeatAttribute 28 | ///// 29 | ///// The number of times to run the test 30 | //public RepeatAttribute(int count) 31 | //{ 32 | // this.count = count; 33 | //} 34 | 35 | ///// 36 | ///// Gets the number of times to run the test. 37 | ///// 38 | //public int Count 39 | //{ 40 | // get { return count; } 41 | //} 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/RequiredAddinAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2008, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// RequiredAddinAttribute may be used to indicate the names of any addins 13 | /// that must be present in order to run some or all of the tests in an 14 | /// assembly. If the addin is not loaded, the entire assembly is marked 15 | /// as NotRunnable. 16 | /// 17 | [AttributeUsage(AttributeTargets.Assembly,AllowMultiple=true)] 18 | public class RequiredAddinAttribute : Attribute 19 | { 20 | private string requiredAddin; 21 | 22 | /// 23 | /// Initializes a new instance of the class. 24 | /// 25 | /// The required addin. 26 | public RequiredAddinAttribute(string requiredAddin) 27 | { 28 | this.requiredAddin = requiredAddin; 29 | } 30 | 31 | /// 32 | /// Gets the name of required addin. 33 | /// 34 | /// The required addin name. 35 | public string RequiredAddin 36 | { 37 | get { return requiredAddin; } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/SetCultureAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// Summary description for SetCultureAttribute. 13 | /// 14 | [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Assembly, AllowMultiple=true)] 15 | public class SetCultureAttribute : PropertyAttribute 16 | { 17 | /// 18 | /// Construct given the name of a culture 19 | /// 20 | /// 21 | public SetCultureAttribute( string culture ) : base( "_SETCULTURE", culture ) { } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/SetUICultureAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// Summary description for SetUICultureAttribute. 13 | /// 14 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Assembly, AllowMultiple = true)] 15 | public class SetUICultureAttribute : PropertyAttribute 16 | { 17 | /// 18 | /// Construct given the name of a culture 19 | /// 20 | /// 21 | public SetUICultureAttribute(string culture) : base("_SETUICULTURE", culture) { } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/SetUpAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | 11 | /// 12 | /// Attribute used to mark a class that contains one-time SetUp 13 | /// and/or TearDown methods that apply to all the tests in a 14 | /// namespace or an assembly. 15 | /// 16 | [AttributeUsage(AttributeTargets.Method, AllowMultiple=false)] 17 | public class SetUpAttribute : Attribute 18 | {} 19 | } 20 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/SetUpFixtureAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2007, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// SetUpFixtureAttribute is used to identify a SetUpFixture 13 | /// 14 | [AttributeUsage(AttributeTargets.Class, AllowMultiple=false)] 15 | public class SetUpFixtureAttribute : Attribute 16 | { 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/SpecialValue.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2009, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// The SpecialValue enum is used to represent TestCase arguments 13 | /// that cannot be used as arguments to an Attribute. 14 | /// 15 | public enum SpecialValue 16 | { 17 | /// 18 | /// Null represents a null value, which cannot be used as an 19 | /// argument to an attriute under .NET 1.x 20 | /// 21 | Null 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/SuccessException.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | using System.Runtime.Serialization; 11 | 12 | /// 13 | /// Thrown when an assertion failed. 14 | /// 15 | [Serializable] 16 | public class SuccessException : System.Exception 17 | { 18 | /// 19 | public SuccessException(string message) 20 | : base(message) 21 | { } 22 | 23 | /// The error message that explains 24 | /// the reason for the exception 25 | /// The exception that caused the 26 | /// current exception 27 | public SuccessException(string message, Exception inner) 28 | : 29 | base(message, inner) 30 | { } 31 | 32 | /// 33 | /// Serialization Constructor 34 | /// 35 | protected SuccessException(SerializationInfo info, 36 | StreamingContext context) 37 | : base(info, context) 38 | { } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/SuiteAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | 11 | /// 12 | /// Attribute used to mark a static (shared in VB) property 13 | /// that returns a list of tests. 14 | /// 15 | [AttributeUsage(AttributeTargets.Property, AllowMultiple=false)] 16 | public class SuiteAttribute : Attribute 17 | {} 18 | } 19 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/TearDownAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | 11 | /// 12 | /// Attribute used to identify a method that is called 13 | /// immediately after each test is run. The method is 14 | /// guaranteed to be called, even if an exception is thrown. 15 | /// 16 | [AttributeUsage(AttributeTargets.Method, AllowMultiple=false)] 17 | public class TearDownAttribute : Attribute 18 | {} 19 | } 20 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/TestAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | 11 | /// 12 | /// Adding this attribute to a method within a 13 | /// class makes the method callable from the NUnit test runner. There is a property 14 | /// called Description which is optional which you can provide a more detailed test 15 | /// description. This class cannot be inherited. 16 | /// 17 | /// 18 | /// 19 | /// [TestFixture] 20 | /// public class Fixture 21 | /// { 22 | /// [Test] 23 | /// public void MethodToTest() 24 | /// {} 25 | /// 26 | /// [Test(Description = "more detailed description")] 27 | /// publc void TestDescriptionMethod() 28 | /// {} 29 | /// } 30 | /// 31 | /// 32 | [AttributeUsage(AttributeTargets.Method, AllowMultiple=false)] 33 | public class TestAttribute : Attribute 34 | { 35 | private string description; 36 | 37 | /// 38 | /// Descriptive text for this test 39 | /// 40 | public string Description 41 | { 42 | get { return description; } 43 | set { description = value; } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/TestCaseSourceAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2008, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// FactoryAttribute indicates the source to be used to 13 | /// provide test cases for a test method. 14 | /// 15 | [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)] 16 | public class TestCaseSourceAttribute : Attribute 17 | { 18 | private readonly string sourceName; 19 | private readonly Type sourceType; 20 | 21 | /// 22 | /// Construct with the name of the factory - for use with languages 23 | /// that don't support params arrays. 24 | /// 25 | /// An array of the names of the factories that will provide data 26 | public TestCaseSourceAttribute(string sourceName) 27 | { 28 | this.sourceName = sourceName; 29 | } 30 | 31 | /// 32 | /// Construct with a Type and name - for use with languages 33 | /// that don't support params arrays. 34 | /// 35 | /// The Type that will provide data 36 | /// The name of the method, property or field that will provide data 37 | public TestCaseSourceAttribute(Type sourceType, string sourceName) 38 | { 39 | this.sourceType = sourceType; 40 | this.sourceName = sourceName; 41 | } 42 | 43 | /// 44 | /// The name of a the method, property or fiend to be used as a source 45 | /// 46 | public string SourceName 47 | { 48 | get { return sourceName; } 49 | } 50 | 51 | /// 52 | /// A Type to be used as a source 53 | /// 54 | public Type SourceType 55 | { 56 | get { return sourceType; } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/TestFixtureSetUpAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | 11 | /// 12 | /// Attribute used to identify a method that is 13 | /// called before any tests in a fixture are run. 14 | /// 15 | [AttributeUsage(AttributeTargets.Method, AllowMultiple=false)] 16 | public class TestFixtureSetUpAttribute : Attribute 17 | { 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/TestFixtureTearDownAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | 11 | /// 12 | /// Attribute used to identify a method that is called after 13 | /// all the tests in a fixture have run. The method is 14 | /// guaranteed to be called, even if an exception is thrown. 15 | /// 16 | [AttributeUsage(AttributeTargets.Method, AllowMultiple=false)] 17 | public class TestFixtureTearDownAttribute : Attribute 18 | { 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/TheoryAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // This is free software licensed under the NUnit license. You 3 | // may obtain a copy of the license as well as information regarding 4 | // copyright ownership at http://nunit.org. 5 | // **************************************************************** 6 | 7 | namespace NUnit.Framework 8 | { 9 | using System; 10 | 11 | /// 12 | /// Adding this attribute to a method within a 13 | /// class makes the method callable from the NUnit test runner. There is a property 14 | /// called Description which is optional which you can provide a more detailed test 15 | /// description. This class cannot be inherited. 16 | /// 17 | /// 18 | /// 19 | /// [TestFixture] 20 | /// public class Fixture 21 | /// { 22 | /// [Test] 23 | /// public void MethodToTest() 24 | /// {} 25 | /// 26 | /// [Test(Description = "more detailed description")] 27 | /// publc void TestDescriptionMethod() 28 | /// {} 29 | /// } 30 | /// 31 | /// 32 | [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] 33 | public class TheoryAttribute : Attribute 34 | { 35 | //private string description; 36 | 37 | ///// 38 | ///// Descriptive text for this test 39 | ///// 40 | //public string Description 41 | //{ 42 | // get { return description; } 43 | // set { description = value; } 44 | //} 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Silverlight/NUnit.SL/framework.sl/ValueSourceAttribute.cs: -------------------------------------------------------------------------------- 1 | // **************************************************************** 2 | // Copyright 2008, Charlie Poole 3 | // This is free software licensed under the NUnit license. You may 4 | // obtain a copy of the license at http://nunit.org 5 | // **************************************************************** 6 | 7 | using System; 8 | 9 | namespace NUnit.Framework 10 | { 11 | /// 12 | /// ValueSourceAttribute indicates the source to be used to 13 | /// provide data for one parameter of a test method. 14 | /// 15 | [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true, Inherited = false)] 16 | public class ValueSourceAttribute : Attribute 17 | { 18 | private readonly string sourceName; 19 | private readonly Type sourceType; 20 | 21 | /// 22 | /// Construct with the name of the factory - for use with languages 23 | /// that don't support params arrays. 24 | /// 25 | /// The name of the data source to be used 26 | public ValueSourceAttribute(string sourceName) 27 | { 28 | this.sourceName = sourceName; 29 | } 30 | 31 | /// 32 | /// Construct with a Type and name - for use with languages 33 | /// that don't support params arrays. 34 | /// 35 | /// The Type that will provide data 36 | /// The name of the method, property or field that will provide data 37 | public ValueSourceAttribute(Type sourceType, string sourceName) 38 | { 39 | this.sourceType = sourceType; 40 | this.sourceName = sourceName; 41 | } 42 | 43 | /// 44 | /// The name of a the method, property or fiend to be used as a source 45 | /// 46 | public string SourceName 47 | { 48 | get { return sourceName; } 49 | } 50 | 51 | /// 52 | /// A Type to be used as a source 53 | /// 54 | public Type SourceType 55 | { 56 | get { return sourceType; } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Silverlight/SL.Specific/ApplicationException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace EmitMapper 4 | { 5 | public class ApplicationException : Exception 6 | { 7 | public ApplicationException() 8 | { 9 | } 10 | 11 | public ApplicationException(string message) : base(message) 12 | { 13 | } 14 | 15 | public ApplicationException(string message, Exception innerException) : base(message, innerException) 16 | { 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Silverlight/SL.Specific/Convert.cs: -------------------------------------------------------------------------------- 1 | using System.Globalization; 2 | 3 | namespace System 4 | { 5 | public static class ConvertSL 6 | { 7 | public static object ChangeType(object from, Type typeTo) 8 | { 9 | return Convert.ChangeType(from, typeTo, CultureInfo.CurrentUICulture); 10 | } 11 | } 12 | 13 | public static class EnumSL 14 | { 15 | public static object Parse(Type objectType, string str) 16 | { 17 | return Enum.Parse(objectType, str, true); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Silverlight/SL.Specific/Environment.cs: -------------------------------------------------------------------------------- 1 | using System.Globalization; 2 | using System.Runtime.Versioning; 3 | 4 | namespace System 5 | { 6 | public class EnvironmentX 7 | { 8 | [ResourceExposure(ResourceScope.None)] 9 | internal static String GetResourceString(String key) 10 | { 11 | //return GetResourceFromDefault(key); 12 | return key; 13 | } 14 | 15 | [ResourceExposure(ResourceScope.None)] 16 | internal static String GetResourceString(String key, params Object[] values) 17 | { 18 | //String s = GetResourceFromDefault(key); 19 | string s = key; 20 | return String.Format(CultureInfo.CurrentCulture, s, values); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Silverlight/SL.Specific/ICloneable.cs: -------------------------------------------------------------------------------- 1 | // Type: System.ICloneable 2 | // Assembly: mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 3 | // Assembly location: C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll 4 | 5 | using System.Runtime.InteropServices; 6 | 7 | namespace System 8 | { 9 | [ComVisible(true)] 10 | public interface ICloneable 11 | { 12 | object Clone(); 13 | } 14 | } -------------------------------------------------------------------------------- /Silverlight/SL.Specific/IFormatterConverter.cs: -------------------------------------------------------------------------------- 1 | // Type: System.Runtime.Serialization.IFormatterConverter 2 | // Assembly: mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 3 | // Assembly location: C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll 4 | 5 | using System; 6 | using System.Runtime.InteropServices; 7 | 8 | namespace System.Runtime.Serialization 9 | { 10 | public interface IFormatterConverter 11 | { 12 | object Convert(object value, Type type); 13 | object Convert(object value, TypeCode typeCode); 14 | bool ToBoolean(object value); 15 | char ToChar(object value); 16 | sbyte ToSByte(object value); 17 | byte ToByte(object value); 18 | short ToInt16(object value); 19 | ushort ToUInt16(object value); 20 | int ToInt32(object value); 21 | uint ToUInt32(object value); 22 | long ToInt64(object value); 23 | ulong ToUInt64(object value); 24 | float ToSingle(object value); 25 | double ToDouble(object value); 26 | decimal ToDecimal(object value); 27 | DateTime ToDateTime(object value); 28 | string ToString(object value); 29 | } 30 | } -------------------------------------------------------------------------------- /Silverlight/SL.Specific/ISerializable.cs: -------------------------------------------------------------------------------- 1 | // Type: System.Runtime.Serialization.ISerializable 2 | // Assembly: mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 3 | // Assembly location: C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll 4 | 5 | using System.Runtime.InteropServices; 6 | 7 | namespace System.Runtime.Serialization 8 | { 9 | [ComVisible(true)] 10 | public interface ISerializable 11 | { 12 | void GetObjectData(SerializationInfo info, StreamingContext context); 13 | } 14 | } -------------------------------------------------------------------------------- /Silverlight/TestsLauncher.SL.Web/Default.aspx: -------------------------------------------------------------------------------- 1 | <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestsLauncher.SL.Web._Default" %> 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 |
12 | 13 |
14 |
15 | 16 | 17 | -------------------------------------------------------------------------------- /Silverlight/TestsLauncher.SL.Web/Default.aspx.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.UI; 6 | using System.Web.UI.WebControls; 7 | 8 | namespace TestsLauncher.SL.Web 9 | { 10 | public partial class _Default : System.Web.UI.Page 11 | { 12 | protected void Page_Load(object sender, EventArgs e) 13 | { 14 | 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Silverlight/TestsLauncher.SL.Web/Default.aspx.designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:2.0.50727.42 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace TestsLauncher.SL.Web 12 | { 13 | 14 | 15 | public partial class _Default 16 | { 17 | 18 | /// 19 | /// form1 control. 20 | /// 21 | /// 22 | /// Auto-generated field. 23 | /// To modify move field declaration from designer file to code-behind file. 24 | /// 25 | protected global::System.Web.UI.HtmlControls.HtmlForm form1; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Silverlight/TestsLauncher.SL.Web/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("TestsLauncher.SL.Web")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("TestsLauncher.SL.Web")] 13 | [assembly: AssemblyCopyright("Copyright © 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("3d5900ae-111a-45be-96b3-d9e4606ca793")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Revision and Build Numbers 33 | // by using the '*' as shown below: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /Silverlight/TestsLauncher.SL/App.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Silverlight/TestsLauncher.SL/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Net; 5 | using System.Windows; 6 | using System.Windows.Controls; 7 | using System.Windows.Documents; 8 | using System.Windows.Input; 9 | using System.Windows.Media; 10 | using System.Windows.Media.Animation; 11 | using System.Windows.Shapes; 12 | 13 | namespace TestsLauncher.SL 14 | { 15 | public partial class App : Application 16 | { 17 | 18 | public App() 19 | { 20 | this.Startup += this.Application_Startup; 21 | this.Exit += this.Application_Exit; 22 | this.UnhandledException += this.Application_UnhandledException; 23 | 24 | InitializeComponent(); 25 | } 26 | 27 | private void Application_Startup(object sender, StartupEventArgs e) 28 | { 29 | this.RootVisual = new MainPage(); 30 | } 31 | 32 | private void Application_Exit(object sender, EventArgs e) 33 | { 34 | 35 | } 36 | private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) 37 | { 38 | // If the app is running outside of the debugger then report the exception using 39 | // the browser's exception mechanism. On IE this will display it a yellow alert 40 | // icon in the status bar and Firefox will display a script error. 41 | if (!System.Diagnostics.Debugger.IsAttached) 42 | { 43 | 44 | // NOTE: This will allow the application to continue running after an exception has been thrown 45 | // but not handled. 46 | // For production applications this error handling should be replaced with something that will 47 | // report the error to the website and stop the application. 48 | e.Handled = true; 49 | Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); }); 50 | } 51 | } 52 | private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e) 53 | { 54 | try 55 | { 56 | string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace; 57 | errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n"); 58 | 59 | System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");"); 60 | } 61 | catch (Exception) 62 | { 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Silverlight/TestsLauncher.SL/MainPage.xaml: -------------------------------------------------------------------------------- 1 |  6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Silverlight/TestsLauncher.SL/MainPage.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Net; 5 | using System.Windows; 6 | using System.Windows.Controls; 7 | using System.Windows.Documents; 8 | using System.Windows.Input; 9 | using System.Windows.Media; 10 | using System.Windows.Media.Animation; 11 | using System.Windows.Shapes; 12 | using System.Text; 13 | using System.Reflection; 14 | using System.Windows.Resources; 15 | using System.IO; 16 | using NUnit.Framework; 17 | 18 | namespace TestsLauncher.SL 19 | { 20 | public partial class MainPage : UserControl 21 | { 22 | public MainPage() 23 | { 24 | InitializeComponent(); 25 | 26 | var result = new StringBuilder(); 27 | 28 | StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("EmitMapperTests.SL.dll", UriKind.Relative)); 29 | var testFixtures = new AssemblyPart() 30 | .Load(streamInfo.Stream) 31 | .GetTypes() 32 | .Where(t => t.GetCustomAttributes(typeof(TestFixtureAttribute), false).Length > 0 ) 33 | .ToArray(); 34 | 35 | int cntAll = 0; 36 | int cntPassed = 0; 37 | foreach (var t in testFixtures) 38 | { 39 | var obj = Activator.CreateInstance(t); 40 | foreach (var mi in 41 | t.GetMethods() 42 | .Where(m => m.GetCustomAttributes(typeof(TestAttribute), false).Length > 0 && m.GetParameters().Length == 0) 43 | ) 44 | { 45 | result.Append("Executing " + t.ToString() + "." + mi.Name + "... "); 46 | try 47 | { 48 | cntAll++; 49 | mi.Invoke(obj, null); 50 | cntPassed++; 51 | result.AppendLine("OK"); 52 | } 53 | catch (Exception e) 54 | { 55 | result.AppendLine("ERROR"); 56 | result.AppendLine(e.ToString()); 57 | result.AppendLine("--------------------------------"); 58 | } 59 | } 60 | } 61 | 62 | result.AppendLine("Done. " + cntPassed + "(" + cntAll + ")"); 63 | 64 | txtOut.Text = result.ToString(); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Silverlight/TestsLauncher.SL/Properties/AppManifest.xml: -------------------------------------------------------------------------------- 1 |  4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Silverlight/TestsLauncher.SL/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("TestsLauncher.SL")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("TestsLauncher.SL")] 13 | [assembly: AssemblyCopyright("Copyright © 2009")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("dd598f22-a37c-42a1-8b5c-bf4e7fe11daa")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Revision and Build Numbers 33 | // by using the '*' as shown below: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /libs/BenchmarkLibs/AutoMapper.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/BenchmarkLibs/AutoMapper.dll -------------------------------------------------------------------------------- /libs/BenchmarkLibs/BLToolkit.3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/BenchmarkLibs/BLToolkit.3.dll -------------------------------------------------------------------------------- /libs/BenchmarkLibs/LinFu.DynamicProxy.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/BenchmarkLibs/LinFu.DynamicProxy.dll -------------------------------------------------------------------------------- /libs/NUnit/lib/Failure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/Failure.png -------------------------------------------------------------------------------- /libs/NUnit/lib/Ignored.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/Ignored.png -------------------------------------------------------------------------------- /libs/NUnit/lib/Inconclusive.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/Inconclusive.png -------------------------------------------------------------------------------- /libs/NUnit/lib/Skipped.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/Skipped.png -------------------------------------------------------------------------------- /libs/NUnit/lib/Success.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/Success.png -------------------------------------------------------------------------------- /libs/NUnit/lib/nunit-gui-runner.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/nunit-gui-runner.dll -------------------------------------------------------------------------------- /libs/NUnit/lib/nunit.core.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/nunit.core.dll -------------------------------------------------------------------------------- /libs/NUnit/lib/nunit.core.interfaces.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/nunit.core.interfaces.dll -------------------------------------------------------------------------------- /libs/NUnit/lib/nunit.fixtures.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/nunit.fixtures.dll -------------------------------------------------------------------------------- /libs/NUnit/lib/nunit.uiexception.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/nunit.uiexception.dll -------------------------------------------------------------------------------- /libs/NUnit/lib/nunit.uikit.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/nunit.uikit.dll -------------------------------------------------------------------------------- /libs/NUnit/lib/nunit.util.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/lib/nunit.util.dll -------------------------------------------------------------------------------- /libs/NUnit/nunit.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/nunit.exe -------------------------------------------------------------------------------- /libs/NUnit/nunit.framework.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MetSystem/EmitMapper/d8c2913a94eaa2c8a7754c24de587f39d85c613a/libs/NUnit/nunit.framework.dll -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Project Description 2 | Powerful customisable tool for mapping entities to each other. Entities can be plain objects, DataReaders, SQL commands and anything you need. The tool uses run-time code generation via the Emit library. It is usefull for dealing with DTO objects, data access layers an so on. 3 | ## Supported platforms: 4 | 5 | * Microsoft .NET Framework 3.5 6 | * Microsoft Silverlight 3 7 | * Mono 8 | ## About Emit Mapper 9 | 10 | * Overview 11 | * Benefits of Emit Mapper 12 | * Getting started 13 | * Type conversion 14 | * Customization 15 | 16 | # Customization overview 17 | 18 | Customization using default configurator 19 | * Default configurator overview 20 | * Custom converters 21 | * Custom converters_for_generics 22 | * Null substitution 23 | * Ignoring members 24 | * Custom constructors 25 | * Shallow and_deep_mapping 26 | * Names matching 27 | * Post processing 28 | 29 | Low-level customization using custom configuratorors 30 | # Emit Mapper in practice. 31 | 32 | * Benchmark: EmitMapper vs Handwritten code vs AutoMapper 33 | * Objects change tracking 34 | * Mapping DbDatareader to objects 35 | * Mapping objects to DbCommand (UPDATE and INSERT) 36 | Last edited Jan 11, 2010 at 3:01 PM by romankovs, version 25 37 | from http://emitmapper.codeplex.com/ --------------------------------------------------------------------------------