• 查询视图扩展方法


    public static class DatabaseExtensions
    {
    public static IEnumerable SqlQueryForDynamic(this Database db,
    string sql,
    params object[] parameters)
    {
    IDbConnection defaultConn = new System.Data.SqlClient.SqlConnection();

    return SqlQueryForDynamicOtherDB(db, sql, defaultConn, parameters);
    }

    public static IEnumerable SqlQueryForDynamicOtherDB(this Database db,
    string sql,
    IDbConnection conn,
    params object[] parameters)
    {
    conn.ConnectionString = db.Connection.ConnectionString;

    if (conn.State != ConnectionState.Open)
    {
    conn.Open();
    }

    IDbCommand cmd = conn.CreateCommand();
    cmd.CommandText = sql;

    IDataReader dataReader = cmd.ExecuteReader();

    if (!dataReader.Read())
    {
    return null; //无结果返回Null
    }

    #region 构建动态字段

    TypeBuilder builder = DatabaseExtensions.CreateTypeBuilder(
    "EF_DynamicModelAssembly",
    "DynamicModule",
    "DynamicType");

    int fieldCount = dataReader.FieldCount;
    for (int i = 0; i < fieldCount; i++)
    {
    //dic.Add(i, dataReader.GetName(i));

    //Type type = dataReader.GetFieldType(i);

    DatabaseExtensions.CreateAutoImplementedProperty(
    builder,
    dataReader.GetName(i),
    dataReader.GetFieldType(i));
    }

    #endregion

    dataReader.Close();
    dataReader.Dispose();
    cmd.Dispose();
    conn.Close();
    conn.Dispose();

    Type returnType = builder.CreateType();

    if (parameters != null)
    {
    return db.SqlQuery(returnType, sql, parameters);
    }
    else
    {
    return db.SqlQuery(returnType, sql);
    }
    }

    public static TypeBuilder CreateTypeBuilder(string assemblyName,
    string moduleName,
    string typeName)
    {
    TypeBuilder typeBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
    new AssemblyName(assemblyName),
    AssemblyBuilderAccess.Run).DefineDynamicModule(moduleName).DefineType(typeName,
    TypeAttributes.Public);
    typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
    return typeBuilder;
    }

    public static void CreateAutoImplementedProperty(
    TypeBuilder builder,
    string propertyName,
    Type propertyType)
    {
    const string PrivateFieldPrefix = "m_";
    const string GetterPrefix = "get_";
    const string SetterPrefix = "set_";

    // Generate the field.
    FieldBuilder fieldBuilder = builder.DefineField(
    string.Concat(
    PrivateFieldPrefix, propertyName),
    propertyType,
    FieldAttributes.Private);

    // Generate the property
    PropertyBuilder propertyBuilder = builder.DefineProperty(
    propertyName,
    System.Reflection.PropertyAttributes.HasDefault,
    propertyType, null);

    // Property getter and setter attributes.
    MethodAttributes propertyMethodAttributes = MethodAttributes.Public
    | MethodAttributes.SpecialName
    | MethodAttributes.HideBySig;

    // Define the getter method.
    MethodBuilder getterMethod = builder.DefineMethod(
    string.Concat(
    GetterPrefix, propertyName),
    propertyMethodAttributes,
    propertyType,
    Type.EmptyTypes);

    // Emit the IL code.
    // ldarg.0
    // ldfld,_field
    // ret
    ILGenerator getterILCode = getterMethod.GetILGenerator();
    getterILCode.Emit(OpCodes.Ldarg_0);
    getterILCode.Emit(OpCodes.Ldfld, fieldBuilder);
    getterILCode.Emit(OpCodes.Ret);

    // Define the setter method.
    MethodBuilder setterMethod = builder.DefineMethod(
    string.Concat(SetterPrefix, propertyName),
    propertyMethodAttributes,
    null,
    new Type[] { propertyType });

    // Emit the IL code.
    // ldarg.0
    // ldarg.1
    // stfld,_field
    // ret
    ILGenerator setterILCode = setterMethod.GetILGenerator();
    setterILCode.Emit(OpCodes.Ldarg_0);
    setterILCode.Emit(OpCodes.Ldarg_1);
    setterILCode.Emit(OpCodes.Stfld, fieldBuilder);
    setterILCode.Emit(OpCodes.Ret);

    propertyBuilder.SetGetMethod(getterMethod);
    propertyBuilder.SetSetMethod(setterMethod);
    }

    }
  • 相关阅读:
    檢查 cpu 的全部 gpio 狀態及設定
    device tree 負值 property 寫法
    shell script timer and 無限迴圈
    vim 搜尋取代功能
    英国人是这样“造”单词的
    计划时间程序
    程序猿的崛起,一篇文章看懂编程语言
    如何读懂计算机(二进制)
    为什么计算机上能看到动人的图片,精彩的视频和悦耳的音乐?(二进制)
    格式化时间用了YYYY-MM-dd,元旦当天老板喊我回去改Bug!(转载)
  • 原文地址:https://www.cnblogs.com/wahaccp/p/3250363.html
Copyright © 2020-2023  润新知