获得Class中最高的财产价值

时间:2017-07-20 19:07:42

标签: c#

我正试图在班上获得最多的所有项目:

public class aClass{ 

        public int PropA{ get; set; } = 1;
        public int PropB{ get; set; } = 18;
        public int PropC{ get; set; } = 25; 
}  

这是我的代码:

public int GetMaxConfiguratableColumns()
        {
            int _HighestNumber = 0;
            PropertyInfo[] _Info = this.GetType().GetProperties(); 
            foreach(PropertyInfo _PropretyInfo in _Info)
            {
                //I'm lost here!!!!!!!
            }
            return _HighestNumber;
        }

有什么建议吗?谢谢!

4 个答案:

答案 0 :(得分:1)

如果它不必使用反射,我可以建议这样的事情吗?

public class aClass
{
    public int PropA { get; set; } = 1;
    public int PropB { get; set; } = 18;
    public int PropC { get; set; } = 25;

    public int GetMaxConfiguratableColumns()
    {
        return new List<int> {this.PropA, this.PropB, this.PropC}.Max();
    }
}

答案 1 :(得分:0)

使用_PropretyInfo.GetValue(myObject)获取当前属性的值,然后将其存储在temp变量中并迭代并将temp值与其余值进行比较

答案 2 :(得分:0)

我想你最终会有这样的事情:

        int highestNumber = 0;      

        PropertyInfo[] info = this.GetType().GetProperties(); 
        foreach(PropertyInfo propInfo in info)
        {
            if (propInfo.PropertyType == typeof(int))
            {
                int propValue = (int)(propInfo.GetValue(this, null));
                if (propValue > highestNumber) {
                    highestNumber = propValue;
                }
            }
        }

        return highestNumber;

答案 3 :(得分:0)

得到了答案:

USE [MENUdb]


ALTER FUNCTION [dbo].[func_GetMenuItemDescriptionForMenuItemID]

(
@MENUITEMID int
)

RETURNS nvarchar(MAX)
AS
BEGIN
RETURN 
(
SELECT  MenuItem.MenuItemTitle +'  ' + MenuItem.MenuItemDescriptionText FROM MenuItem WHERE MenuItemID = @MENUITEMID
)
END


USE [MENUdb]
GO


ALTER Procedure [dbo].[sp_GetMenuItemDescriptionForMenuItemID]

(
@ENTER_MENUITEMID int
)

AS
BEGIN
SELECT dbo.func_GetMenuItemDescriptionForMenuItemID (@ENTER_MENUITEMID)
END

无论谁需要这个。