如何从Generic T值中获取值 [英] how to fetch values from Generic T value

查看:91
本文介绍了如何从Generic T值中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1,

var objDel = new BendEntity
            {
                Bend_ManualInput_Id = Convert.ToInt32(gvBendManual.DataKeys[e.RowIndex].Value.ToString()),
                Created_by = Convert.ToInt32(Session["UserId"])

            };

            BendService.DeleteBendUserManualInput<bendentity>(objDel);  ---------> here this is generic parameter passed





-------



2,





-------

2,

public static int DeleteBendUserManualInput<t>(T value)  ---------> here this is generic parameter passed
     {
         BendProvider obj_prov = new BendProvider();
         return obj_prov.DeleteBendUserManualInput<t>(value);  ---------> here this is generic parameter passed
     }





--------------------------------



3,





--------------------------------

3,

 public int DeleteBendUserManualInput<t>(T value) ---------> here this is generic parameter passed
        {
            //objEntity = objInfo as BlankEntity;
            
            string val = value.ToString();
}





--------------------



在上面的第一种方法中,一个类实体对象作为通用参数传递



但是如何检索来自第三种方法的值...........T值



但是在调试时我可以看到T值中的值为



Bend_ManualInput_Id = 2



sheet_Thickness = null

No_of_Strokes = null

Max_Bend_Length = null

V_Die_Opening = null

Min_Flange_Length = null



i需要从第三种方法中的T值中检索Bend_ManualInput_Id属性的值





--------------------

in the above in 1st method the a class entity object is passed as generic parameter

But how to retrieve values from the third method from ........... T value

But while debugging i can ble to see the the values in T Value as

Bend_ManualInput_Id = 2

sheet_Thickness = null
No_of_Strokes = null
Max_Bend_Length = null
V_Die_Opening = null
Min_Flange_Length = null

i need to retrieve values from Bend_ManualInput_Id property from T values in third method

Public class BendEntity
{

        public int         Bend_ManualInput_Id   { get; set; }
        public double  Sheet_Thickness        { get; set; }
        public int         No_of_Strokes { get; set; }
        public double  Max_Bend_Length { get; set; }
        public int         V_Die_Opening { get; set; }
        public int         Min_Flange_Length { get; set; }

}





添加了代码块,删除了虚假的结束标签 - OriginalGriff [/ edit]



[edit]Code block added, spurious closing tags removed - OriginalGriff[/edit]

推荐答案

你好anbujeramiah,



正如Zoltan所说,坚持.NET惯例会对你有好处代码(也是属性的命名 - 不要在公共属性名称中使用下划线 - 大多数.NET开发人员都遵循这个明确的建议。曾经在.NET Framework中看到过这样的属性名称吗?


无论如何 - 对你的问题:



't'(应该是'T')可以是任何类型吗?那么对通用定义没有约束?如果您可以使用约束,则可以安全地将通用值重新设置为约束类型或接口,并以正常方式调用属性。(如果您这样做,使用is和as关键字可能很有用; - )如果你不得不期望任何类型,你可以使用反射或开关作为可能的类型(如果值是TypeX,否则如果值是TypeY,...)。但是我是不推荐这个。 (使用动态也可能是一种可能性,但同样风险很大......)



我不得不问:为什么Delete方法需要访问已删除对象的属性(我确定你有充分的理由 - 只是想知道......)?
Hi anbujeramiah,

As Zoltan said, sticking to .NET conventions would be good for your code (also the naming of your properties - don't use underscores in public property names - a clear advice most .NET developers follow. Ever seen such property names in the .NET Framework?

Anyway - to your Problem:

't' (should be 'T') can be any type? So no constrains on the generic definition? If you could use constrains you can safely recast the generic value to the constrained type or interface and call the properties in a "normal" way. (if you do this, using of "is" and "as" keywords can be usefull ;-) If you have to expect any type you could use reflection or a "switch" for the possible types (if value is TypeX, else if value is TypeY, ...). But I wouldn't recommend this. (using "dynamic" could also be a possibillity, but again, quite risky...)

I have to ask: why does a Delete method needs access to the properties of the deleted object (I'm shure you have good reason for it - just wondering...)?


这篇关于如何从Generic T值中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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