Пример #1
0
        public void ReadWriteTypeTestComplex()
        {
            DomType input = new DomType();

            input.Name      = "Test";
            input.ClassType = ClassType.Struct;
            input.BaseType  = new DomReturnType("BaseClass");
            input.AddInterfaceImplementation(new DomReturnType("Interface1"));
            input.AddInterfaceImplementation(new DomReturnType("Interface2"));

            input.Add(new DomMethod("TestMethod", Modifiers.None, MethodModifier.None, DomLocation.Empty, DomRegion.Empty));
            input.Add(new DomMethod(".ctor", Modifiers.None, MethodModifier.IsConstructor, DomLocation.Empty, DomRegion.Empty));

            input.Add(new DomField("TestField", Modifiers.None, DomLocation.Empty, DomReturnType.Void));
            input.Add(new DomProperty("TestProperty", Modifiers.None, DomLocation.Empty, DomRegion.Empty, DomReturnType.Void));
            input.Add(new DomEvent("TestEvent", Modifiers.None, DomLocation.Empty, DomReturnType.Void));
            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, input);
            byte[] bytes = ms.ToArray();

            DomType result = DomPersistence.ReadType(CreateReader(bytes), DefaultNameDecoder, null);

            Assert.AreEqual("Test", result.Name);
            Assert.AreEqual(ClassType.Struct, result.ClassType);
            Assert.AreEqual("BaseClass", result.BaseType.Name);
            Assert.AreEqual(1, result.MethodCount);
            Assert.AreEqual(1, result.ConstructorCount);
            Assert.AreEqual(1, result.FieldCount);
            Assert.AreEqual(1, result.PropertyCount);
            Assert.AreEqual(1, result.EventCount);
        }
Пример #2
0
        public bool AddReferenceFromPersistenceFolder(string assemblyName, bool doAsync = false)
        {
            if (!Directory.Exists(GetPersistencePath()))
            {
                Directory.CreateDirectory(GetPersistencePath());
            }

            bool result = false;

            RunMethod(
                delegate
            {
                lock (_contentRegistry)
                {
                    DomPersistence persistence = _contentRegistry.ActivatePersistence(GetPersistencePath());

                    IProjectContent persistenceContent = persistence.LoadProjectContentByAssemblyName(assemblyName);
                    if (null != persistenceContent)
                    {
                        _cacheReferences[assemblyName] = persistenceContent;
                        //_projectContent.AddReferencedContent(persistenceContent);
                        _projectContent.ReferencedContents.Add(persistenceContent);
                        result = true;
                    }
                }
            }, doAsync);
            return(result);
        }
Пример #3
0
        public void AddReferencesFromPersistenceFolder(string[] assemblyNames, bool doAsync = false)
        {
            RunMethod(
                delegate
            {
                lock (_contentRegistry)
                {
                    if (!Directory.Exists(GetPersistencePath()))
                    {
                        Directory.CreateDirectory(GetPersistencePath());
                    }

                    foreach (string item in assemblyNames)
                    {
                        DomPersistence persistence = _contentRegistry.ActivatePersistence(GetPersistencePath());

                        if (_cacheReferences.ContainsKey(item))
                        {
                            continue;
                        }

                        IProjectContent persistenceContent = persistence.LoadProjectContentByAssemblyName(item);
                        if (null != persistenceContent)
                        {
                            _cacheReferences[item] = persistenceContent;
                            _projectContent.ReferencedContents.Add(persistenceContent);
                            //_projectContent.AddReferencedContent(persistenceContent);
                        }
                    }
                }
            }, doAsync);
        }
Пример #4
0
        public void ReadWriteReturnTypeTest()
        {
            DomReturnType input = new DomReturnType();

            input.Name                = "Test";
            input.Namespace           = "Namespace";
            input.ArrayDimensions     = 5;
            input.IsByRef             = true;
            input.IsNullable          = true;
            input.PointerNestingLevel = 666;
            input.AddTypeParameter(new DomReturnType("System.String"));
            input.AddTypeParameter(new DomReturnType("System.Int32"));
            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, input);
            byte[] bytes = ms.ToArray();

            IReturnType result = DomPersistence.ReadReturnType(CreateReader(bytes), DefaultNameDecoder, null);

            Assert.AreEqual("Test", result.Name);
            Assert.AreEqual("Namespace", result.Namespace);
            Assert.AreEqual("Namespace.Test", result.FullName);
            Assert.AreEqual(5, result.ArrayDimensions);
            Assert.AreEqual(true, result.IsByRef);
            Assert.AreEqual(true, result.IsNullable);
            Assert.AreEqual("System.String", result.GenericArguments[0].FullName);
            Assert.AreEqual("System.Int32", result.GenericArguments[1].FullName);
        }
        public ReflectionWithRoundTripLayerTests()
        {
            mscorlib = AssemblyParserService.DefaultProjectContentRegistry.Mscorlib;

            MemoryStream memory = new MemoryStream();

            DomPersistence.WriteProjectContent((ReflectionProjectContent)mscorlib, memory);
            memory.Position = 0;
            mscorlib        = DomPersistence.LoadProjectContent(memory, AssemblyParserService.DefaultProjectContentRegistry);
        }
        public ReflectionWithRoundTripLayerTests()
        {
            mscorlib = new ProjectContentRegistry().Mscorlib;

            MemoryStream memory = new MemoryStream();

            DomPersistence.WriteProjectContent((ReflectionProjectContent)mscorlib, memory);
            memory.Position = 0;
            mscorlib        = DomPersistence.LoadProjectContent(memory, new ProjectContentRegistry());
        }
Пример #7
0
        public void ReadWriteAttributeTest()
        {
            DomAttribute attr = new DomAttribute();

            CodePropertyReferenceExpression exp1 = new CodePropertyReferenceExpression();

            exp1.TargetObject = new CodeTypeReferenceExpression("SomeType");
            exp1.PropertyName = "SomeProperty";

            CodeTypeOfExpression exp2 = new CodeTypeOfExpression("SomeTypeOf");

            CodeBinaryOperatorExpression exp3 = new CodeBinaryOperatorExpression();

            exp3.Left     = new CodePrimitiveExpression("one");
            exp3.Right    = new CodePrimitiveExpression("two");
            exp3.Operator = CodeBinaryOperatorType.Add;

            CodePrimitiveExpression exp4 = new CodePrimitiveExpression(37);

            attr.AddPositionalArgument(exp1);
            attr.AddPositionalArgument(exp2);
            attr.AddPositionalArgument(exp3);
            attr.AddPositionalArgument(exp4);

            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, attr);
            byte[]       bytes  = ms.ToArray();
            DomAttribute result = DomPersistence.ReadAttribute(CreateReader(bytes), DefaultNameDecoder, null);

            Assert.AreEqual(4, result.PositionalArguments.Count);

            Assert.AreEqual(typeof(CodePropertyReferenceExpression), result.PositionalArguments [0].GetType());
            CodePropertyReferenceExpression rexp1 = (CodePropertyReferenceExpression)result.PositionalArguments [0];

            Assert.AreEqual(typeof(CodeTypeReferenceExpression), rexp1.TargetObject.GetType());
            Assert.AreEqual("SomeType", ((CodeTypeReferenceExpression)rexp1.TargetObject).Type.BaseType);
            Assert.AreEqual("SomeProperty", rexp1.PropertyName);

            Assert.AreEqual(typeof(CodeTypeOfExpression), result.PositionalArguments [1].GetType());
            Assert.AreEqual("SomeTypeOf", ((CodeTypeOfExpression)result.PositionalArguments [1]).Type.BaseType);

            Assert.AreEqual(typeof(CodeBinaryOperatorExpression), result.PositionalArguments [2].GetType());
            CodeBinaryOperatorExpression rexp3 = (CodeBinaryOperatorExpression)result.PositionalArguments [2];

            Assert.AreEqual(typeof(CodePrimitiveExpression), rexp3.Left.GetType());
            Assert.AreEqual("one", ((CodePrimitiveExpression)rexp3.Left).Value);
            Assert.AreEqual(typeof(CodePrimitiveExpression), rexp3.Right.GetType());
            Assert.AreEqual("two", ((CodePrimitiveExpression)rexp3.Right).Value);

            Assert.AreEqual(typeof(CodePrimitiveExpression), result.PositionalArguments [3].GetType());
            Assert.AreEqual(37, ((CodePrimitiveExpression)result.PositionalArguments [3]).Value);
        }
        protected override IEnumerable <IAttribute> GetAssemblyAttributes(Assembly assembly)
        {
            var pc = new ReflectionProjectContent("TestName", "testlocation", new DomAssemblyName[0], new ProjectContentRegistry());

            pc.AddAssemblyAttributes(assembly);

            MemoryStream memory = new MemoryStream();

            DomPersistence.WriteProjectContent(pc, memory);

            memory.Position = 0;
            return(DomPersistence.LoadProjectContent(memory, new ProjectContentRegistry()).GetAssemblyAttributes());
        }
Пример #9
0
        protected override IClass GetClass(Type type)
        {
            ICompilationUnit cu = new ReflectionProjectContent("TestName", "testlocation", new DomAssemblyName[0], ParserService.DefaultProjectContentRegistry).AssemblyCompilationUnit;
            IClass           c  = new ReflectionClass(cu, type, type.FullName, null);

            cu.ProjectContent.AddClassToNamespaceList(c);

            MemoryStream memory = new MemoryStream();

            DomPersistence.WriteProjectContent((ReflectionProjectContent)c.ProjectContent, memory);

            memory.Position = 0;
            return(DomPersistence.LoadProjectContent(memory, ParserService.DefaultProjectContentRegistry).Classes.Single());
        }
Пример #10
0
        public void ReadWriteLocationTest()
        {
            DomLocation  input  = new DomLocation(3, 9);
            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, null, input);
            byte[] bytes = ms.ToArray();

            DomLocation result = DomPersistence.ReadLocation(CreateReader(bytes), null);

            Assert.AreEqual(3, result.Line);
            Assert.AreEqual(9, result.Column);
        }
Пример #11
0
        public void ReadWriteDelegateTest()
        {
            DomType input = DomType.CreateDelegate(null, "TestDelegate", new DomLocation(10, 10), DomReturnType.Void, new List <IParameter> ());

            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, input);
            byte[] bytes = ms.ToArray();

            DomType result = DomPersistence.ReadType(CreateReader(bytes), DefaultNameDecoder, null);

            Assert.AreEqual("TestDelegate", result.Name);
            Assert.AreEqual(ClassType.Delegate, result.ClassType);
        }
Пример #12
0
        public void AddReferencesFromFile(string[] assemblyNames, string[] assemblyLocations, bool tryPersistence = true, bool doAsync = false)
        {
            if (!Directory.Exists(GetPersistencePath()))
            {
                Directory.CreateDirectory(GetPersistencePath());
            }

            RunMethod(
                delegate
            {
                lock (_contentRegistry)
                {
                    DomPersistence persistence = _contentRegistry.ActivatePersistence(GetPersistencePath());

                    for (int i = 0; i < assemblyNames.Length; i++)
                    {
                        string assemblyName     = assemblyNames[i];
                        string assemblyLocation = assemblyLocations[i];

                        if (_cacheReferences.ContainsKey(assemblyName))
                        {
                            continue;
                        }

                        if (true == tryPersistence)
                        {
                            IProjectContent persistenceContent = persistence.LoadProjectContentByAssemblyName(assemblyName);
                            if (null != persistenceContent)
                            {
                                _cacheReferences[assemblyName] = persistenceContent;
                                //_projectContent.AddReferencedContent(persistenceContent);
                                _projectContent.ReferencedContents.Add(persistenceContent);

                                continue;
                            }
                        }

                        IProjectContent result = _contentRegistry.GetProjectContentForReference(assemblyName, assemblyLocation);
                        if (null != result)
                        {
                            _cacheReferences[assemblyName] = result;
                            _projectContent.ReferencedContents.Add(result);
                            // _projectContent.AddReferencedContent(result);
                        }
                    }
                }
            }, doAsync);
        }
Пример #13
0
        public void ReadWriteRegionTest()
        {
            DomRegion input = new DomRegion(1, 2, 3, 4);

            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, input);
            byte[] bytes = ms.ToArray();

            DomRegion result = DomPersistence.ReadRegion(CreateReader(bytes), DefaultNameDecoder);

            Assert.AreEqual(1, result.Start.Line);
            Assert.AreEqual(2, result.Start.Column);
            Assert.AreEqual(3, result.End.Line);
            Assert.AreEqual(4, result.End.Column);
        }
        protected override IClass GetClass(Type type)
        {
            ICompilationUnit cu = new ReflectionProjectContent("TestName", "testlocation", new DomAssemblyName[0], new ProjectContentRegistry()).AssemblyCompilationUnit;
            IClass           c  = new ReflectionClass(cu, type, type.FullName, null);

            cu.ProjectContent.AddClassToNamespaceList(c);

            MemoryStream memory = new MemoryStream();

            DomPersistence.WriteProjectContent((ReflectionProjectContent)c.ProjectContent, memory);

            memory.Position = 0;
            ReflectionProjectContent loadedPC = DomPersistence.LoadProjectContent(memory, new ProjectContentRegistry());

            loadedPC.AddReferencedContent(mscorlib);
            return(loadedPC.Classes.Single());
        }
Пример #15
0
        public void ReflectionParserTest()
        {
            ICompilationUnit cu = new ReflectionProjectContent("TestName", "testlocation", new AssemblyName[0], ParserService.DefaultProjectContentRegistry).AssemblyCompilationUnit;
            IClass           c  = new ReflectionClass(cu, typeof(TestClass <,>), typeof(TestClass <,>).FullName, null);

            cu.ProjectContent.AddClassToNamespaceList(c);

            CheckClass(c);
            MemoryStream memory = new MemoryStream();

            DomPersistence.WriteProjectContent((ReflectionProjectContent)cu.ProjectContent, memory);

            memory.Position = 0;
            foreach (IClass c2 in DomPersistence.LoadProjectContent(memory, ParserService.DefaultProjectContentRegistry).Classes)
            {
                CheckClass(c2);
            }
        }
Пример #16
0
        public void ReadWritePropertyTest()
        {
            DomProperty input = new DomProperty();

            input.Name             = "Test";
            input.PropertyModifier = PropertyModifier.IsIndexer | PropertyModifier.HasGet | PropertyModifier.HasSet;

            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, input);
            byte[] bytes = ms.ToArray();

            DomProperty result = DomPersistence.ReadProperty(CreateReader(bytes), DefaultNameDecoder, null);

            Assert.AreEqual("Test", result.Name);
            Assert.AreEqual(true, result.IsIndexer);
            Assert.AreEqual(true, result.HasGet);
            Assert.AreEqual(true, result.HasSet);
        }
Пример #17
0
        public void ReadWriteTypeTest()
        {
            DomType input = new DomType();

            input.Name      = "Test";
            input.ClassType = ClassType.Struct;
            input.BaseType  = new DomReturnType("BaseClass");

            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, input);
            byte[] bytes = ms.ToArray();

            DomType result = DomPersistence.ReadType(CreateReader(bytes), DefaultNameDecoder, null);

            Assert.AreEqual("Test", result.Name);
            Assert.AreEqual(ClassType.Struct, result.ClassType);
            Assert.AreEqual("BaseClass", result.BaseType.Name);
        }
Пример #18
0
        public void AddWellKnownReferenceFromFile(string assemblyName, string assemblyLocation, bool tryPersistence = true, bool doAsync = false)
        {
            if (_cacheReferences.ContainsKey(assemblyName))
            {
                return;
            }

            if (!Directory.Exists(GetPersistencePath()))
            {
                Directory.CreateDirectory(GetPersistencePath());
            }

            RunMethod(
                delegate
            {
                lock (_contentRegistry)
                {
                    DomPersistence persistence = _contentRegistry.ActivatePersistence(GetPersistencePath());

                    if (true == tryPersistence)
                    {
                        IProjectContent persistenceContent = persistence.LoadProjectContentByAssemblyName(assemblyName);
                        if (null != persistenceContent)
                        {
                            _cacheReferences[assemblyName] = persistenceContent;
                            //_projectContent.AddReferencedContent(persistenceContent);
                            _projectContent.ReferencedContents.Add(persistenceContent);
                            return;
                        }
                    }

                    IProjectContent result = _contentRegistry.GetProjectContentForReference(assemblyName, assemblyLocation);
                    if (null != result)
                    {
                        _cacheReferences[assemblyName] = result;
                        _projectContent.ReferencedContents.Add(result);
                        //_projectContent.AddReferencedContent(result);
                    }
                }
            }, doAsync);
        }
Пример #19
0
        public void ReadWriteEventTest()
        {
            DomEvent input = new DomEvent();

            input.Name         = "Test";
            input.AddMethod    = new DomMethod("AddMethod", Modifiers.New, MethodModifier.None, DomLocation.Empty, DomRegion.Empty);
            input.RemoveMethod = new DomMethod("RemoveMethod", Modifiers.New, MethodModifier.None, DomLocation.Empty, DomRegion.Empty);
            input.RaiseMethod  = new DomMethod("RaiseMethod", Modifiers.New, MethodModifier.None, DomLocation.Empty, DomRegion.Empty);

            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, input);
            byte[] bytes = ms.ToArray();

            DomEvent result = DomPersistence.ReadEvent(CreateReader(bytes), DefaultNameDecoder, null);

            Assert.AreEqual("Test", result.Name);
            Assert.AreEqual("AddMethod", result.AddMethod.Name);
            Assert.AreEqual("RemoveMethod", result.RemoveMethod.Name);
            Assert.AreEqual("RaiseMethod", result.RaiseMethod.Name);
        }
Пример #20
0
        public void ReadWriteMethodTest()
        {
            DomMethod input = new DomMethod();

            input.Name           = "Test";
            input.MethodModifier = MethodModifier.IsConstructor;
            input.Add(new DomParameter(input, "par1", DomReturnType.Void));
            input.AddTypeParameter(new TypeParameter("T"));
            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, input);
            byte[] bytes = ms.ToArray();

            DomMethod result = DomPersistence.ReadMethod(CreateReader(bytes), DefaultNameDecoder, null);

            Assert.AreEqual("Test", result.Name);
            Assert.AreEqual(true, result.IsConstructor);
            Assert.AreEqual("par1", result.Parameters [0].Name);
            Assert.AreEqual("Void", result.Parameters [0].ReturnType.Name);
            Assert.AreEqual(1, result.TypeParameters.Count);
            Assert.AreEqual("T", result.TypeParameters [0].Name);
        }
Пример #21
0
        public void ReadWriteFieldTest()
        {
            DomField input = new DomField();

            input.Name          = "TestField";
            input.Location      = new DomLocation(5, 10);
            input.Documentation = "testDocumentation";
            input.Modifiers     = Modifiers.Static;
            input.ReturnType    = new DomReturnType("System.String");

            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, input);
            byte[] bytes = ms.ToArray();

            DomField result = DomPersistence.ReadField(CreateReader(bytes), DefaultNameDecoder, null);

            Assert.AreEqual("TestField", result.Name);
            Assert.AreEqual("testDocumentation", result.Documentation);
            Assert.AreEqual(new DomLocation(5, 10), result.Location);
            Assert.AreEqual(Modifiers.Static, result.Modifiers);
            Assert.AreEqual("System.String", result.ReturnType.FullName);
        }
Пример #22
0
        public void ReadWriteFieldTest2()
        {
            DomField input = new DomField();

            input.Name          = null;
            input.Location      = DomLocation.Empty;
            input.Documentation = null;
            input.Modifiers     = Modifiers.None;
            input.ReturnType    = null;

            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            DomPersistence.Write(writer, DefaultNameEncoder, input);
            byte[] bytes = ms.ToArray();

            DomField result = DomPersistence.ReadField(CreateReader(bytes), DefaultNameDecoder, null);

            Assert.AreEqual(null, result.Name);
            Assert.AreEqual(null, result.Documentation);
            Assert.AreEqual(DomLocation.Empty, result.Location);
            Assert.AreEqual(Modifiers.None, result.Modifiers);
            Assert.AreEqual(null, result.ReturnType);
        }