从属性信息对象作为通用传递属性类型 [英] Passing a Property Type from Property Info object as a Generic

查看:72
本文介绍了从属性信息对象作为通用传递属性类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这一点,我正在做一个项目,目的是在我们的代码库中扩展泛型的使用,以提高我们代码中某些部分的可重用性.  

我有一个函数,可以评估所有基本系统类型并进行比较.但是,关于模型和其他客户类别,我目前仅使用To String比较.  我们通常会覆盖To String函数 这样至少可以比较该模型或自定义数据类型的几个字段.  

从属性信息对象获取类型并将其设置为函数泛型的正确方法是什么.下面是我们当前正在使用的类,请注意,为了简洁起见,我删除了大多数基本系统类型.  

开关(pA.GetValue(A,null).GetType().ToString().ToLower().Replace("system.",")))
{
    //为简洁起见,删除了代码
    案例布尔":
    情况布尔值":
        bool b1 = Convert.ToBoolean(pA.GetValue(A,null)),b2 = Convert.ToBoolean(pB.GetValue(B,null));

        如果(b1!= b2)返回false;
        休息;
    案例"char":
        char c1 = Convert.ToChar(pA.GetValue(A,null)),c2 = Convert.ToChar(pA.GetValue(A,null));

        如果(((char)pA.GetValue(A,null))!=((charcharpB.GetValue(B,null)))返回false;
        休息;
    大小写字符串":
        字符串str1 = Convert.ToString(pA.GetValue(A,null)),str2 = Convert.ToString(pB.GetValue(B,null));

        如果(str1!= str2)返回false;
        休息;
    默认:
        字符串dtStr1 = pA.GetValue(A,null).ToString(),dtStr2 = pA.GetValue(B,null).ToString();

        如果(dtStr1!= dtStr2)返回false;
        休息;
} 

我就是说,如果我们可以从属性信息中提取类型,那么我们就可以利用递归原理.

请先感谢您的想法.

解决方案

我的想法是:您正在做的事情似乎是正确的. ..危险,因为它完全绕过了类型安全性,效率很低,我只是看不到它到底如何增加了可重用性"!

如果要说服我这实际上在做些有用的事情,您将不得不给我更多的背景信息. (至少该方法的其余部分包含您的代码以及如何调用它的示例).

通过定义,您应该已经知道每种基本类型的类型.如果类中的某个属性应包含一个布尔值,则该布尔值应被声明为布尔值.

如果您需要比较该布尔值,则只需...进行比较!

如果(b1 == b2){...做某事...} 

编写相似的代码来比较两个不同的布尔值几乎不是代码重复.

您到底要解决什么问题?



A little background on this, I am working on a project to expand the use of generics in our code base to increase the reusability of some parts of our code.  

I have a function that that evaluates all base system types and does their comparisons.  However, when it comes to Models and other Customer Classes, I am currently just using the To String comparison.  We typically override the To String function so that it at least compares a few fields of that model or custom data type.  

What is the correct way to get the Type from a Property Info object and set it as the generic of a function.  Below is the class we are using currently, please note, I have removed most of the base system types for brevity.  

switch (pA.GetValue(A, null).GetType().ToString().ToLower().Replace("system.", ""))
{
    //Code Removed for brevity
    case "bool":
    case "boolean":
        bool b1 = Convert.ToBoolean(pA.GetValue(A, null)), b2 = Convert.ToBoolean(pB.GetValue(B, null));

        if (b1 != b2) return false;
        break;
    case "char":
        char c1 = Convert.ToChar(pA.GetValue(A, null)), c2 = Convert.ToChar(pA.GetValue(A, null));

        if (((char)pA.GetValue(A, null)) != ((char)pB.GetValue(B, null))) return false;
        break;
    case "string":
        string str1 = Convert.ToString(pA.GetValue(A, null)), str2 = Convert.ToString(pB.GetValue(B, null));

        if (str1 != str2) return false;
        break;
    default:
        string dtStr1 = pA.GetValue(A, null).ToString(), dtStr2 = pA.GetValue(B, null).ToString();

        if (dtStr1 != dtStr2) return false;
        break;
}

I am that if we can pull the type from the property info, that we can utilize the recursive principle.

Thank you in advance for your thoughts. 

解决方案

My thoughts are: what you are doing seems to be rather...dangerous, in that it completely bypasses type-safety, is inefficient, and I just can't see how on earth it "increases reusability"!

You're gonna have to give me far more context if you are to convince me that this is actually doing something useful. (At least the rest of the method that contains your code and an example of how it is called).

You should already know the type of every base type by definition. If a property in a class should hold a boolean than it is hopefully declared as a bool.

And if you need to compare that bool then you just...compare it!

if (b1 == b2) { ... do something ...}

Writing similar code to compare two different boolean values is hardly code duplication.

Exactly what problem are you really trying to solve?



这篇关于从属性信息对象作为通用传递属性类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆