示例#1
0
        public TypeSymbol GetTypeSymbol(Type systemType, AbstractPhaseContext context)
        {
            int arrayDepth = 0;

            while (systemType.IsArray)
            {
                arrayDepth++;
                systemType = systemType.GetElementType();
            }

            ITypeSymbol typeSymbol = RoslynCompilation.GetTypeByMetadataName(systemType.FullName);

            for (int i = 0; i < arrayDepth; ++i)
            {
                typeSymbol = RoslynCompilation.CreateArrayTypeSymbol(typeSymbol, 1);
            }

            return(GetTypeSymbol(typeSymbol, context));
        }
示例#2
0
 public SemanticModel GetSemanticModel(SyntaxTree modelTree)
 {
     return(RoslynCompilation.GetSemanticModel(modelTree));
 }
示例#3
0
 public TypeSymbol GetTypeSymbol(SpecialType type, AbstractPhaseContext context)
 {
     return(GetTypeSymbol(RoslynCompilation.GetSpecialType(type), context));
 }
示例#4
0
        public static TestConcurrentApp Build(string code,
                                              out IEnumerable <Diagnostic> errors,
                                              bool withInterface = false,
                                              bool withRemote    = false)
        {
            errors = null;

            var config = MockInjector(new Options
            {
                GenerateInterface = withInterface,
                GenerateRemote    = withRemote,
            });
            var compilation = new RoslynCompilation();

            compilation.addDocument("concurrent-test", code, config);

            Assembly assembly = compilation.build();

            if (assembly == null)
            {
                errors = compilation.errors();

                //debug
                StringBuilder errorLines = new StringBuilder();
                foreach (var error in errors)
                {
                    errorLines.AppendLine(error.ToString());
                }

                var errorString = errorLines.ToString();
                return(null);
            }

            var types  = new FactoryMap();
            var result = new TestConcurrentApp(types, new DefaultInstantiator());

            foreach (var type in assembly.GetTypes())
            {
                var attributes = type.CustomAttributes;
                if (!attributes.Any(attr => attr.AttributeType == typeof(ConcurrentAttribute)))
                {
                    continue;
                }

                var typeName = type.ToString();
                if (attributes.Any(attr => attr.AttributeType == typeof(ConcurrentSingleton)))
                {
                    result.AddSingleton(typeName, (IConcurrentObject)Activator.CreateInstance(type));
                    continue;
                }

                var useParameterLess = type.GetConstructors().Length == 0;
                if (!useParameterLess)
                {
                    useParameterLess = type.GetConstructor(new Type[] { }) != null;
                }

                types[typeName] = (app, args) =>
                {
                    if (useParameterLess)
                    {
                        return((IConcurrentObject)Activator.CreateInstance(type));
                    }

                    var ctor = type.GetConstructor(args
                                                   .Select(arg => arg.GetType())
                                                   .ToArray());

                    if (ctor != null)
                    {
                        return((IConcurrentObject)ctor.Invoke(args));
                    }

                    throw new InvalidOperationException("unable to find a constructor");
                };
            }

            return(result);
        }