C#反射getValue用来替换某些字符?

发布于 2025-01-25 11:48:40 字数 1078 浏览 2 评论 0原文

使用.NET 5,我有两个同一类列表的列表。一个是从SQL数据库中从实体框架中生成的,另一个是使用Oracles DatamAnagedAccess库(如果很重要的话)生成的。在这一点上,根据与调试器所显示的内容,相关的字符串是正确的。

            foreach (var PairedCustomer in PairedCustomers)
            {
                bool MakeProp = false;
                //compare properties we care about (defined in mapping)
                foreach (string prop in PropertiesWeCareAbout)
                {
                    string tProp = PairedCustomer.TargetCustomer.GetType().GetProperty(prop).GetValue(PairedCustomer.TargetCustomer, null).ToString(); 
                    string dProp = PairedCustomer.DCustomer.GetType().GetProperty(prop).GetValue(PairedCustomer.DCustomer, null).ToString();

在上面的代码中,问题是什么是“ pairedcustomer.targetcustomer”在对象中的字符串值为“fürfischerei”,但是,tprop值显示为:“ f?r fischerei”。它使用所有Umlaut和类似角色来完成此操作,但仅适用于来自Oracle的列表。为什么在列表中(通过VS调试器)看起来正确,但被替换为?当使用反射时(是的...我讨厌使用反射,但是目前我没有更好的方法)。

Using .NET 5, I have two Lists of the same class List. One was generated from Entity Framework from a SQL database and the other using Oracles DataManagedAccess library (if it matters) from a Oracle database. At this point, per what VS debugger shows, the strings in question are correct.

            foreach (var PairedCustomer in PairedCustomers)
            {
                bool MakeProp = false;
                //compare properties we care about (defined in mapping)
                foreach (string prop in PropertiesWeCareAbout)
                {
                    string tProp = PairedCustomer.TargetCustomer.GetType().GetProperty(prop).GetValue(PairedCustomer.TargetCustomer, null).ToString(); 
                    string dProp = PairedCustomer.DCustomer.GetType().GetProperty(prop).GetValue(PairedCustomer.DCustomer, null).ToString();

What the issue is, in the above code "PairedCustomer.TargetCustomer" has a string value in the object as "für Fischerei" however, the tProp value is showing up as: "f?r Fischerei". It does this with all umlaut and similar characters but only for the List that came from Oracle. Why does it appear correct in the List (via VS debugger), but is replaced with ? when using reflection (Yes... I hate using reflection, but I don't have a better way at the moment).

enter image description here

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

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

发布评论

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

评论(1

烟织青萝梦 2025-02-01 11:48:40

我终于解决了。 Oracle响应使用扩展的ASCII字符。解决方案是使用编码:encoding.getEncoding(1252).getString(tprop.Select(c =>(byte)c).toArray()).getString());

I finally solved it. The Oracle response is using extended Ascii characters. Solution was to convert the value using encoding: Encoding.GetEncoding(1252).GetString(tProp.Select(c => (byte)c).ToArray());

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