从数据库中调用字符串值

发布于 2025-02-03 00:53:54 字数 315 浏览 4 评论 0原文

因此,我的Oracle表中有一个单元格,其值将是某些系统函数或某些静态属性,例如datetime.now.year
在表单元格中,类型为varchar2.net其字符串中。

如何为每种情况(属性,静态函数)生成一个通用代码以获取字符串中的值

string str = "DateTime.Now.Year" // the value comes from DataBase
var valueFromDataBase = str.invoke();

So I have a cell in my Oracle table, its value will be some System function or some Static Property like DateTime.Now.Year.
at the table cell the type is VARCHAR2 in .Net its string.

How can I produce a Generic Code for each case (Property, static Function) to get the value in the string

string str = "DateTime.Now.Year" // the value comes from DataBase
var valueFromDataBase = str.invoke();

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

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

发布评论

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

评论(1

欢你一世 2025-02-10 00:53:54

考虑到您正在使用反射存储类型和属性,

public class Entity
{
    public string TypeName { get; set; }
    public string PropertyName { get; set; }
}
new Entity { TypeName = typeof(DateTime).FullName ; PropertyName = nameof(DateTime.Now) }

您现在可以扭转该过程

public static object? GetPropertyValue(string typeName, string propertyName)
{
    var assemblies = AppDomain.CurrentDomain.GetAssemblies();
    var types = assemblies.SelectMany(a => a.GetTypes());
    
    var type = types.First(t => t.FullName == typeName);
    var property = type
        .GetProperties(BindingFlags.Public | BindingFlags.Static)
        .First(p => p.Name == propertyName);

    return property.GetValue(null);
}
var datetime = GetPropertyValue(entityFromDb.TypeName, entityFromDb.PropertyName);

Considering you're storing a type and a property

public class Entity
{
    public string TypeName { get; set; }
    public string PropertyName { get; set; }
}
new Entity { TypeName = typeof(DateTime).FullName ; PropertyName = nameof(DateTime.Now) }

using reflection you can now reverse the process

public static object? GetPropertyValue(string typeName, string propertyName)
{
    var assemblies = AppDomain.CurrentDomain.GetAssemblies();
    var types = assemblies.SelectMany(a => a.GetTypes());
    
    var type = types.First(t => t.FullName == typeName);
    var property = type
        .GetProperties(BindingFlags.Public | BindingFlags.Static)
        .First(p => p.Name == propertyName);

    return property.GetValue(null);
}
var datetime = GetPropertyValue(entityFromDb.TypeName, entityFromDb.PropertyName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文