Пример #1
0
        protected virtual void ValidateFragmentMethod(CompositeValidationResult result, CompositeModel compositeModel, CompositeMethodModel compositeMethod, AbstractFragmentMethodModel methodModel)
        {
            if (compositeMethod == null)
            {
                result.InternalValidationErrors.Add(ValidationErrorFactory.NewInternalError("Composite method model may not be null.", compositeModel));
            }
            if (methodModel != null)
            {
                var declType = methodModel.NativeInfo.DeclaringType;
                if (!declType
#if WINDOWS_PHONE_APP
                    .GetTypeInfo()
#endif
                    .IsClass)
                {
                    result.StructureValidationErrors.Add(ValidationErrorFactory.NewStructureError("The declaring type of fragment method " + methodModel.NativeInfo + " must be a class, however " + declType + " is not a class.", compositeModel, compositeMethod));
                }
                if (!methodModel.IsGeneric && (!methodModel.NativeInfo.IsVirtual || methodModel.NativeInfo.IsFinal))
                {
                    result.StructureValidationErrors.Add(ValidationErrorFactory.NewStructureError("The method " + methodModel.NativeInfo + " in " + declType + " is not virtual, however, all composite methods must be virtual.", compositeModel, methodModel));
                }
                if (declType
#if WINDOWS_PHONE_APP
                    .GetTypeInfo()
#endif
                    .IsSealed)
                {
                    result.StructureValidationErrors.Add(ValidationErrorFactory.NewStructureError("The type " + declType + " is sealed, however, all fragment types must be non-sealed.", compositeModel, methodModel));
                }
                //if ( !declType.IsPublic && !declType.IsNestedPublic )
                //{
                //   String msg = null;
                //   if ( methodModel.NativeInfo.IsAssembly || methodModel.NativeInfo.IsFamilyOrAssembly )
                //   {
                //      if ( !declType.Assembly.GetCustomAttributes( true ).OfType<InternalsVisibleToAttribute>().Any( attr => Qi4CSGeneratedAssemblyAttribute.ASSEMBLY_NAME.Equals( attr.AssemblyName ) ) )
                //      {
                //         msg = "The type " + declType + " is marked as internal, however, the " + typeof( InternalsVisibleToAttribute ) + " with argument " + typeof( Qi4CSGeneratedAssemblyAttribute ) + ".ASSEMBLY_NAME is not applied to the assembly";
                //      }
                //   }
                //   else
                //   {
                //      msg = "The type " + declType + " is not visible to the generated assembly.";
                //   }
                //   if ( msg != null )
                //   {
                //      result.AddStructureError( new StructureValidationErrorImpl( compositeModel, methodModel, msg ) );
                //   }
                //}
                var genName = Qi4CSGeneratedAssemblyAttribute.GetGeneratedAssemblyName(declType.GetAssembly());
                if (!IsTypeVisible(declType, genName))
                {
                    result.StructureValidationErrors.Add(ValidationErrorFactory.NewStructureError("The type " + declType + " is not visible. Consider either making it public, or internal with combination of applying " + typeof(InternalsVisibleToAttribute) + " with argument " + typeof(Qi4CSGeneratedAssemblyAttribute) + ".ASSEMBLY_NAME to the assembly.", compositeModel, methodModel));
                }
            }
        }
Пример #2
0
 protected virtual void ValidateSpecialMethodModel(CompositeValidationResult result, CompositeModel compositeModel, SpecialMethodModel methodModel)
 {
     if (methodModel.NativeInfo.IsGenericMethodDefinition)
     {
         result.StructureValidationErrors.Add(ValidationErrorFactory.NewStructureError("Special methods can not be generic method definitions.", compositeModel, methodModel));
     }
     else
     {
         var declType = methodModel.NativeInfo.DeclaringType;
         var genName  = Qi4CSGeneratedAssemblyAttribute.GetGeneratedAssemblyName(declType.GetAssembly());
         if ((methodModel.NativeInfo.IsAssembly || methodModel.NativeInfo.IsFamilyAndAssembly) &&
             !IsTypeVisible(declType, genName))
         {
             result.StructureValidationErrors.Add(ValidationErrorFactory.NewStructureError("The type " + declType + " is not visible. Consider either making it public, or internal with combination of applying " + typeof(InternalsVisibleToAttribute) + " with argument " + typeof(Qi4CSGeneratedAssemblyAttribute) + ".ASSEMBLY_NAME to the assembly.", compositeModel, methodModel));
         }
         foreach (var pModel in methodModel.Parameters)
         {
             this.ValidateParameter(result, compositeModel, pModel);
         }
     }
 }
        private static Assembly DoLoadGeneratedAssembly(Assembly assembly, ApplicationModel <Qi4CS.Core.SPI.Instance.ApplicationSPI> model, EventHandler <AssemblyLoadingArgs> loadingEvt)
        {
            var args = new AssemblyLoadingArgs(assembly.FullName, Qi4CSGeneratedAssemblyAttribute.GetGeneratedAssemblyName(assembly));

            loadingEvt.InvokeEventIfNotNull(evt => evt(model, args));

            var an = args.Qi4CSGeneratedAssemblyName;

            if (args.Version != null)
            {
                an += ", Version=" + args.Version;
            }

            if (!String.IsNullOrEmpty(args.Culture))
            {
                an += ", Culture=" + args.Culture;
            }

            if (!args.PublicKey.IsNullOrEmpty())
            {
                an += ", PublicKey=" + StringConversions.ByteArray2HexStr(args.PublicKey);
            }
            else if (!args.PublicKeyToken.IsNullOrEmpty())
            {
                an += ", PublicKeyToken=" + StringConversions.ByteArray2HexStr(args.PublicKeyToken);
            }

            return(Assembly.Load(
#if WINDOWS_PHONE_APP
                       new AssemblyName(
#endif
                       an
#if WINDOWS_PHONE_APP
                       )
#endif
                       ));
        }
        private IDictionary <Assembly, CILModule> PerformEmitting(Boolean isSilverlight, CILReflectionContext reflectionContext, out IDictionary <CompositeModel, CompositeEmittingInfo> cResultsOut)
        {
            var typeModelDic    = this._typeModelDic.Value;
            var assembliesArray = this._affectedAssemblies.Value.ToArray();
            var models          = this._models.CQ.Values.ToArray();
            var supports        = this._compositeModelTypeSupport;
            var cResults        = models.ToDictionary(muudel => muudel, muudel => new CompositeEmittingInfo(reflectionContext, models));

            cResultsOut = cResults;
            var codeGens = models
                           .Select(muudel => supports[muudel.ModelType])
                           .Distinct()
                           .ToDictionary(mt => mt.AssemblyScopeSupport.ModelType, mt => Tuple.Create(mt.NewCodeGenerator(isSilverlight, reflectionContext), mt.CodeGenerationInfo));

            var assemblyDic = new Dictionary <Assembly, CILModule>();

            foreach (var currentAssembly in assembliesArray)
            {
                if (!ReflectionHelper.QI4CS_ASSEMBLY.Equals(currentAssembly))
                {
                    var assemblyBareFileName = Qi4CSGeneratedAssemblyAttribute.GetGeneratedAssemblyName(currentAssembly);

                    var ass  = reflectionContext.NewBlankAssembly(assemblyBareFileName);
                    var eAss = currentAssembly.NewWrapper(reflectionContext);
                    ass.Name.MajorVersion = eAss.Name.MajorVersion;
                    ass.Name.MinorVersion = eAss.Name.MinorVersion;
                    ass.Name.BuildNumber  = eAss.Name.BuildNumber;
                    ass.Name.Revision     = eAss.Name.Revision;
                    ass.Name.Culture      = eAss.Name.Culture;

                    ass.AddNewCustomAttributeTypedParams(ASS_TITLE_ATTRIBUTE_CTOR.NewWrapper(reflectionContext), CILCustomAttributeFactory.NewTypedArgument(assemblyBareFileName, reflectionContext));
                    ass.AddNewCustomAttributeTypedParams(ASS_DESCRIPTION_ATTRIBUTE_CTOR.NewWrapper(reflectionContext), CILCustomAttributeFactory.NewTypedArgument((assemblyBareFileName + " Enhanced by Qi4CS."), reflectionContext));
                    //ass.AddNewCustomAttributeTypedParams( ASS_DEFAULT_ALIAS_ATTRIBUTE_CTOR.NewWrapper( reflectionContext ), CILCustomAttributeFactory.NewTypedArgument( assemblyBareFileName, reflectionContext ) );
                    ass.AddNewCustomAttributeTypedParams(QI4CS_GENERATED_ATTRIBUTE_CTOR.NewWrapper(reflectionContext));

                    var mod = ass.AddModule(assemblyBareFileName + ".dll");
                    assemblyDic.Add(currentAssembly, mod);
                }
            }

            // Phase 1: Emit empty types
            System.Threading.Tasks.Parallel.ForEach(assembliesArray, currentAssembly =>
            {
                foreach (var model in models)
                {
                    var tuple1 = codeGens[model.ModelType];
                    var tuple2 = cResults[model];
                    tuple1.Item1.EmitTypesForModel(model, typeModelDic[model], currentAssembly, GetEmittingModule(model, assemblyDic, currentAssembly), tuple1.Item2, tuple2);
                }
            });

            // Phase 2: Emit fragment methods
            System.Threading.Tasks.Parallel.ForEach(assembliesArray, currentAssembly =>
            {
                foreach (var model in models)
                {
                    var tuple1 = codeGens[model.ModelType];
                    var tuple2 = cResults[model];
                    tuple1.Item1.EmitFragmentMethods(model, typeModelDic[model], currentAssembly, tuple1.Item2, tuple2);
                }
            });

            // Phase 3: Emit composite methods and concern & side-effect invocation types
            System.Threading.Tasks.Parallel.ForEach(assembliesArray, currentAssembly =>
            {
                foreach (var model in models)
                {
                    var tuple1 = codeGens[model.ModelType];
                    var tuple2 = cResults[model];
                    tuple1.Item1.EmitCompositeMethosAndInvocationInfos(model, typeModelDic[model], currentAssembly, tuple1.Item2, tuple2);
                }
            });

            // Phase 4: Emit all composite extra methods (state check, pre-prototype, etc)
            System.Threading.Tasks.Parallel.ForEach(assembliesArray, currentAssembly =>
            {
                foreach (var model in models)
                {
                    var tuple1 = codeGens[model.ModelType];
                    var tuple2 = cResults[model];
                    tuple1.Item1.EmitCompositeExtraMethods(model, typeModelDic[model], currentAssembly, tuple1.Item2, tuple2);
                }
            });

            // Phase 5: Emit all composite constructors
            System.Threading.Tasks.Parallel.ForEach(assembliesArray, currentAssembly =>
            {
                foreach (var model in models)
                {
                    var tuple1 = codeGens[model.ModelType];
                    var tuple2 = cResults[model];
                    tuple1.Item1.EmitCompositeConstructors(model, typeModelDic[model], currentAssembly, tuple1.Item2, tuple2);
                }
            });

            // Phase 6: Emit all composite factory types
            System.Threading.Tasks.Parallel.ForEach(assembliesArray, currentAssembly =>
            {
                foreach (var model in models)
                {
                    var tuple1 = codeGens[model.ModelType];
                    var tuple2 = cResults[model];
                    tuple1.Item1.EmitCompositeFactory(model, currentAssembly, GetEmittingModule(model, assemblyDic, currentAssembly), tuple1.Item2, tuple2);
                }
            });

            return(assemblyDic);
        }