惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 赶路人之刚出发

集成WebSecurity的Authorize进行身份验证时,数据库连接报错问题 Html.ActionLink传递参数 Automapper结合EF实现insert,update方法 MVC中使用RemoteAttribute异步远程验证 Html.RenderPartial WebMatrix.WebSecurity创建自定义用户属性 强类型view中List<Model〉问题 ViewBag任意属性的实现方法 params关键字 配置LINQ中的datacontext的log路径,以记录datacontext执行了的查询sql SortedList LINQ join/left join/cross join/group by/group join/sortedlist/cast Linq to objects示例 yield return 和 Func Lamda表达式 IDisposable 对象初始化器和集合初始化器 C#自动属性 .net random伪随机数
匿名类型与扩展方法
赶路人之刚出发 · 2013-04-11 · via 博客园 - 赶路人之刚出发

匿名类型

匿名类型就是没有名字的类型。在C#3.0中允许我们在程序中声明一个临时的类型来存储数据,例如:

var noname = new { name = "Jerry", age = 10 };

编译器编译后的反编译出来的代码为:

View Code

[DebuggerDisplay(@"\{ name = {name}, age = {age} }", Type="<Anonymous Type>"), CompilerGenerated]
internal sealed class <>f__AnonymousType0<<name>j__TPar, <age>j__TPar>
{
    // Fields
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private readonly <age>j__TPar <age>i__Field;
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private readonly <name>j__TPar <name>i__Field;

    // Methods
    [DebuggerHidden]
    public <>f__AnonymousType0(<name>j__TPar name, <age>j__TPar age);
    [DebuggerHidden]
    public override bool Equals(object value);
    [DebuggerHidden]
    public override int GetHashCode();
    [DebuggerHidden]
    public override string ToString();

    // Properties
    public <age>j__TPar age { get; }
    public <name>j__TPar name { get; }
}

其实就是用对象初始化器的做法初始化一个属性都为只读的匿名类型。

在MSDN 中匿名类型的定义是这样的:

   1.匿名类型提供了一种方便的方法,可用来将一组只读属性封装到单个对象中,而无需首先显式定义一个类型。

   2.类型名由编译器生成,并且不能在源代码级使用。每个属性的类型由编译器推断。

   3.可通过使用 new 运算符和对象初始值创建匿名类型。

可通过将隐式键入的本地变量与隐式键入的数组相结合创建匿名键入的元素的数组,如下面的示例所示。

 var anonArray = new[] { new { name = "apple", diam = 4 }, new { name = "grape", diam = 1 }};

扩展方法

View Code

 public class Student
    {
        public int Age { get; set; }
    }
    public static class ExtendMethods
    {
        public static string Hello(this Student stu)
        {
            return "hello extend method" + stu.Age;
        }
    }

 static void Main()
        {
            Student stu = new Student { Age = 10 };
            Console.WriteLine(stu.Hello());
        }

如上例所示,即为Student类增加了一个Hello的扩展方法,效果等同于在Student类中增加了一个静态方法,但该方法只能访问Student类中的公共变量,常用于给第三方库扩展方法。