English 中文(简体)
是否能够利用动力提供通用的物体
原标题:Is it possible to use dynamic to provide generic ToString override for object

我为我的课堂制定方法。 在我看来,有可能将干.转换为动态型,然后使用林克总法和斯特林德为伐木型产出制定标准载体。 不幸的是,我只是刚刚开始了解C#中的动态类型,因此,现在我略微 st。 任何建议?

最佳回答

在实施之后,使用<条码>目标的电离率法,并使用反射法和林克法绘制了所有财产名称和价值的说明。 <Again, Collection/arrays have to be addressed differently.

<>CODE>EDIT 取消所有相关的法典,只保持必要的程度。

public static string ToStringLinq(this object o)
{
    return o.GetType().FullName 
        + Environment.NewLine 
        + string.Join(Environment.NewLine, (from p in o.GetType().GetProperties()
                                            select string.Format("{0}{1}{2}", p.Name,  : , p.GetValue(o, null))));
}

Usage

 AnyClass instance = new AnyClass();
 string toString = instance.ToStringLinq();
问题回答

如果使用LINQ至LQ或实体框架,你必须研究SqlMetal或EF T4模板,以制定海关编码;你可以创建自己的基类,并且所有贵实体都继承这一基类,即采用这种方法。

或者为扩大你的班级而制定一种推广方法,这种推广方法利用思考来做你想要的东西。

HTH.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签