c# 设置 DataContext 列值而无需强类型化

发布于 2024-12-05 20:53:15 字数 1117 浏览 0 评论 0原文

        //Instead of the this 
        var tableX = db.PRODUCT; //db is the DataContext
        //I can do the below (Thanks to http://stackoverflow.com/questions/1919632/get-table-data-from-table-name-in-linq-datacontext
        string tablename = "PRODUCT";
        var table = (ITable)db.GetType().GetProperty(tablename).GetValue(db, null);

        //But instead of this
        PRODUCT _Product = new PRODUCT();
        _Product.PRD_CODE = "code1";
        _Product.PRD_DESC = "description1";
        table.InsertOnSubmit(_Product);
        db.SubmitChanges();

        //How can I do something like this
        string tablename = "PRODUCT";
        var table = (ITable)db.GetType().GetProperty(tablename).GetValue(db, null);
        string lsColumnPrdCode = "PRD_CODE";
        string lsColumnPrdDesc = "PRD_DESC";
        table _tableInstance = new table();
        _tableInstance[lsColumnPrdCode] = "code1";
        _tableInstance[lsColumnPrdDesc] = "description1";
        _tableInstance.InsertOnSubmit(_tableInstance);
        db.SubmitChanges();

那么是否可以在不强类型化的情况下设置数据上下文列值?

        //Instead of the this 
        var tableX = db.PRODUCT; //db is the DataContext
        //I can do the below (Thanks to http://stackoverflow.com/questions/1919632/get-table-data-from-table-name-in-linq-datacontext
        string tablename = "PRODUCT";
        var table = (ITable)db.GetType().GetProperty(tablename).GetValue(db, null);

        //But instead of this
        PRODUCT _Product = new PRODUCT();
        _Product.PRD_CODE = "code1";
        _Product.PRD_DESC = "description1";
        table.InsertOnSubmit(_Product);
        db.SubmitChanges();

        //How can I do something like this
        string tablename = "PRODUCT";
        var table = (ITable)db.GetType().GetProperty(tablename).GetValue(db, null);
        string lsColumnPrdCode = "PRD_CODE";
        string lsColumnPrdDesc = "PRD_DESC";
        table _tableInstance = new table();
        _tableInstance[lsColumnPrdCode] = "code1";
        _tableInstance[lsColumnPrdDesc] = "description1";
        _tableInstance.InsertOnSubmit(_tableInstance);
        db.SubmitChanges();

So it is possible to set datacontext column values without strongly typing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-12-12 20:53:15

当然,你可以使用反射来做这类事情。也许类似于此代码:

Type productType = Type.GetType("PRODUCT");
var product = Activator.CreateInstance(productType);
productType.GetProperty("PRD_CODE").SetValue(product, "code1");
productType.GetProperty("PRD_DESC").SetValue(product, "description1");

Type tableType = table.GetType();
tableType.GetMethod("InsertOnSubmit").Invoke(table, new object[] {product});

但是你为什么要这样做呢?

Of cause you can use reflection to do that kind of stuff. Maybe something similar to this code:

Type productType = Type.GetType("PRODUCT");
var product = Activator.CreateInstance(productType);
productType.GetProperty("PRD_CODE").SetValue(product, "code1");
productType.GetProperty("PRD_DESC").SetValue(product, "description1");

Type tableType = table.GetType();
tableType.GetMethod("InsertOnSubmit").Invoke(table, new object[] {product});

But why do you want to do that?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文