예제 #1
0
        public static CodeStatement BuildAddColumnStatement(Type type, PropertyColumn column)
        {
            Type memberType = TypeReflector.GetMemberType(type, column.PropertyName);

            if (memberType.IsGenericType && memberType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                memberType = memberType.GetGenericArguments()[0];
            }

            CodeMethodInvokeExpression e = new CodeMethodInvokeExpression(new CodeSnippetExpression("dt.Columns"), "Add", new CodeExpression[] {
                new CodePrimitiveExpression(column.ColumnName),
                new CodeTypeOfExpression(memberType)
            });


            return(new CodeExpressionStatement(e));
        }
예제 #2
0
        private static Type BuildAdapter(Type type, string name, IEnumerable <PropertyColumn> columns)
        {
            CodeDomProvider cdp = new Microsoft.CSharp.CSharpCodeProvider();

            CodeCompileUnit    ccu   = BuildCompileUnit(type, name, columns);
            CompilerParameters param = new CompilerParameters();

            List <Type> allTypes = new TypeReflector().GetAllTypes(type);

            foreach (Type t in allTypes)
            {
                string assemblyPath = t.Assembly.Location;
                if (param.ReferencedAssemblies.Contains(assemblyPath))
                {
                    continue;
                }
                param.ReferencedAssemblies.Add(assemblyPath);
            }

            param.ReferencedAssemblies.Add(typeof(IDataTableAdapter <>).Assembly.Location);
            param.ReferencedAssemblies.Add(typeof(DataTable).Assembly.Location);
            param.ReferencedAssemblies.Add(typeof(IListSource).Assembly.Location);
            param.ReferencedAssemblies.Add(typeof(IXmlSerializable).Assembly.Location);

#if DEBUG
            param.TempFiles.KeepFiles     = true;
            param.IncludeDebugInformation = true;
#endif
            CompilerResults cr = cdp.CompileAssemblyFromDom(param, ccu);

            if (cr.Errors.Count > 0)
            {
                foreach (object error in cr.Errors)
                {
                    global::System.Diagnostics.Debug.WriteLine(error);
                }
                throw new Exception(Resources.CodeGenerationError);
            }
            return(cr.CompiledAssembly.GetType(_namespace + "." + name));
        }