一级目录
- LINQ表达式
- 反射type
- 特性
LINQ表达式
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
public class Student
{
public string Name { set; get; }
public int Age { set; get; }
public string GongFu { set; get; }
public override string ToString()
{
// return $"name:{ Name},Age:{Age},GongFu:{ GongFu}";
return string.Format("name:{0},Age:{1},GongFu:{2}", Name, Age, GongFu);
}
}
public class GongFu
{
public string Name { set; get; }
public int ShangHai { set; get; }
public override string ToString()
{
return string.Format("name:{0},ShangHai:{1}", Name,ShangHai);
}
}
namespace 武学功夫
{
class Program
{
static void Main(string[] args)
{
List<Student> str = new List<Student>
{
new Student{Name="小明",Age=18,GongFu="葵花宝典" },
new Student{Name="小红",Age=28,GongFu="祥龙十八掌" },
new Student{Name="小a",Age=18,GongFu="祥龙十八掌" },
new Student{Name="小b",Age=8,GongFu="祥龙十八掌" },
new Student{Name="小c",Age=48,GongFu="祥龙十八掌" },
new Student{Name="老王",Age=113,GongFu="六脉神剑" },
new Student{Name="张三",Age=13,GongFu="葵花点穴手" },
new Student{Name="李四",Age=11,GongFu="金刚不坏" },
};
List<GongFu> fu = new List<GongFu>
{
new GongFu{Name="葵花宝典",ShangHai=86 },
new GongFu{Name="祥龙十八掌",ShangHai=70 },
new GongFu{Name="六脉神剑",ShangHai=99 },
new GongFu{Name="葵花点穴手", ShangHai=46},
new GongFu{Name="金刚不坏", ShangHai=66},
};
//1.查询
//var str1 = from m in str
// from k in fu
// where m.GongFu==k.Name&&k.ShangHai>70
// select new { str=m,fu=k};
//var str1= str.Where(sdsd => sdsd.Age > 50);
// var str1= str.SelectMany(m => fu, (m, k) => new { str = m, fu = k }).Where(x=>x.fu.Name==x.str.GongFu&&x.str.Age>100);//查找
//2.排序
//var str1 = from m in str
// where m.Age >5
// // orderby m.Age,m.Name//多个字段进行排序,先按照年龄,后按照名字
// orderby m.Age descending//从大到小进行排序
// select m;
// var str1 = str.Where(m => m.Age > 50).OrderBy(m => m.Age).ThenBy(m => m.Name);
//倒序OrderByDescending
//3.联合查询
//var str1 = from m in str
// join n in fu on m.GongFu equals n.Name
// select new { str = m, fu = n };
4.对结果进行分组查询
//var str1 = from n in fu
// join m in str on n.Name equals m.GongFu
// into groups
// orderby groups.Count()
// select new { fu = n, count = groups.Count() };
//按照自身字段进行分组group
var str1 = from m in str
group m by m.GongFu into g
select new { count = g.Count() ,key=g.Key};//g.Key表示按照哪个属性进行分的组
//5.量词操作符
//判断是否存在祥龙十八掌
bool a= str.Any(m => m.GongFu== "祥龙十八掌");
Console.WriteLine(a);
//判断是否全部都是祥龙十八掌
bool b =str.All(m => m.GongFu == "祥龙十八掌");
Console.WriteLine(b);
foreach (var item in str1)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
反射type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace 反射和特性type
{
class Program
{
static void Main(string[] args) {
每一个类对应一个type对象,这个type对象存储了这个类 有哪些方法跟哪些数据 哪些成员
// Myclass my = new Myclass();
// Type type = my.GetType();//通过对象来获取这个对象所在类的Type对象
// Console.WriteLine(type.Name);//获取类的名字
// Console.WriteLine(type.Namespace);//获取所在的命名空间
// Console.WriteLine(type.Assembly);
// FieldInfo[] array= type.GetFields();//只能获取pubilc字段
// Console.WriteLine("-----------下面为类的字段-----------");
// foreach(FieldInfo info in array)
// {
// Console.WriteLine(info + " ");
// }
// PropertyInfo[] infos = type.GetProperties();
// Console.WriteLine("-----------下面为类的属性-----------");
// foreach (PropertyInfo info in infos)
// {
// Console.WriteLine(info + " ");
// }
// MethodInfo[] infos1 = type.GetMethods();
// Console.WriteLine("-----------下面为类的方法-----------");
// foreach (MethodInfo info in infos1)
// {
// Console.WriteLine(info + " ");
// }
//通过type对象可以获取它对应的类的所有成员(pubilc)
Myclass my = new Myclass();
Assembly assem = my.GetType().Assembly;//通过类的type对象获取它所在的程序集Assembly
//还可以根据程序集的名字加载程序集
//Assembly assembly1 = Assembly.Load("SomeAssembly");
//根据路径也可以
// Assembly assembly2 = Assembly.LoadFile(@"c:\d\d\");
Console.WriteLine(assem.FullName);
Type[] types= assem.GetTypes();
foreach(Type ty in types)
{
Console.WriteLine(ty);
}
Console.ReadKey();
}
}
}
特性
#define Istext //定义一个宏
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
特性
{//1.特性类的后缀以Attribute结尾
//2.需要继承自System.Attribute
//3.一般情况下声明为sealed
//4.一般情况下 特性类用来表示目标结构的一些状态(定义字段或者属性,一般不定义方法)
[AttributeUsage(AttributeTargets.Class)]//表示该特性类可以应用到的程序结构有哪些
sealed class MytextAttribute : System.Attribute
{
public string A { set; get; }
public int Id { set; get; }
public MytextAttribute(string str)
{
this.A = str;
}
}
}
namespace 特性
{ //通过特定属性的名字,给属性赋值,这种是命名参数
[Mytext("简单的特性类",Id =50)]//当我们使用特性的时候,后面的Attribute不用写
class Program
{//[Obsolete("这个方法过时了,使用NewMethod代替")]//表示一个方法被弃用了,但是仍然可以用
[Obsolete("dd",true)]//这个这个方法就不能被调用了
static void OldMethod()
{
Console.WriteLine("OldMethod");
}
static void NewMethod()
{
Console.WriteLine("NewMethod");
}
[Conditional("Istext")]//若程序开头没有定义Istext的宏,则不会调用text1
static void text1()
{
Console.WriteLine("text1");
}
static void text2()
{
Console.WriteLine("text2");
}
[DebuggerStepThrough]//可以跳过debugger的单步调试 不让进入该方法(当我们确定这个方法没有任何错误的时候,可以用这个)
static void PrintOut(string str,[CallerFilePath]string filename=" ",
[CallerLineNumber] int linenumber=0,[CallerMemberName] string mathodName="")
{
Console.WriteLine(str);
Console.WriteLine(filename);//路径
Console.WriteLine(linenumber);//运行行
Console.WriteLine(mathodName);
}
static void Main(string[] args) {
//
// text1();
// text2();
// text1();
//PrintOut("小明");
Type type = typeof(Program);//通过typeof+类名特可以获取type对象
object[] array= type.GetCustomAttributes(false);
MytextAttribute mytext= array[0] as MytextAttribute;
Console.WriteLine(mytext.A);
Console.WriteLine(mytext.Id);
Console.ReadKey();
}
}