从 Fluently 映射的列名称中检索属性名称

发布于 2024-12-10 17:10:33 字数 740 浏览 0 评论 0原文

背景

我有一个名为“Dog”的类,它引用另一个名为“Tail”的类,该类具有一个名为“Size”的属性。所以,如果我想知道狗的尾巴大小,那就是“Dog.Tail.Size”。完美的。

我用 FluentNHibernate 映射了这个,如下所示:

 public class DogMap : ClassMap<Dog>
 {
    public DogMap()
    { 
        ... other things here
        Component(x => x.Tail, t => {
            t.Map(x => x.Size, "DG_TL_SIZE").Length(2).Not.Nullable();
        }
    }
 }

问题

列名为“DG_TL_SIZE”,我如何获得“Dog.Tail.Size”? 我知道我可以在获得 Dog.Tail.Size 后,获取 "persistentClass.GetRecursiveProperty("Dog.Tail.Size")" 来检索该属性并使用它。

问题是我需要更改它的值,因此我需要从中获取 PropertyInfo,但是当我有“Dog.Tail.Size”时,这应该不难获得。

那么,我怎样才能获得“Dog.Tail.Size”?

Background

I have a class called 'Dog', which references another class called 'Tail', which has a property called 'Size'. So, if i wanted to know the dog's tail size, it would be 'Dog.Tail.Size'. Perfect.

I have this mapped with FluentNHibernate Like this:

 public class DogMap : ClassMap<Dog>
 {
    public DogMap()
    { 
        ... other things here
        Component(x => x.Tail, t => {
            t.Map(x => x.Size, "DG_TL_SIZE").Length(2).Not.Nullable();
        }
    }
 }

The question

Having the column name "DG_TL_SIZE", how can i get "Dog.Tail.Size"?
I know i can, after i have Dog.Tail.Size, get the "persistentClass.GetRecursiveProperty("Dog.Tail.Size")" to retrieve the property and work with it.

The thing is that i need to change its value, therefore i need a PropertyInfo from that, but that should not be hard to get when i have "Dog.Tail.Size".

So, how can i get the "Dog.Tail.Size"?

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

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

发布评论

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

评论(1

等待圉鍢 2024-12-17 17:10:33

您可以迭代所有属性,但所提供的代码需要重构

foreach (var prop in persistentclass.PropertyClosureIterator)
{
    IValue property = prop.Value;
    if (prop.IsComposite)
    {
        var component = (NHibernate.Mapping.Component)prop.Value;

        foreach (var prop2 in component.PropertyIterator)
        {
            foreach (var column in prop2.ColumnIterator)
            {
                if (column.Text == "my Column")
                {
                    // do something with the 'prop2'
                }
            }
        }
    }
    else
    {
        foreach (var column in prop.ColumnIterator)
        {
            if (column.Text == "my Column")
            {
                // do something with the 'prop'
            }
        }
    }
}

you can iterate through all properties, the code presented needs refactoring though

foreach (var prop in persistentclass.PropertyClosureIterator)
{
    IValue property = prop.Value;
    if (prop.IsComposite)
    {
        var component = (NHibernate.Mapping.Component)prop.Value;

        foreach (var prop2 in component.PropertyIterator)
        {
            foreach (var column in prop2.ColumnIterator)
            {
                if (column.Text == "my Column")
                {
                    // do something with the 'prop2'
                }
            }
        }
    }
    else
    {
        foreach (var column in prop.ColumnIterator)
        {
            if (column.Text == "my Column")
            {
                // do something with the 'prop'
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文