版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/It_sharp/article/details/83181639
C#异常
var ae = new ArgumentException();// 参数异常
var ane = new ArgumentNullException();// 参数为空异常
var aore = new ArgumentOutOfRangeException();// 参数超出范围异常
var dne = new DirectoryNotFoundException(); // 文件路径没有找到
var fne = new FileNotFoundException(); //文件没有找到异常
var ioe = new InvalidOperationException(); //非法运算符的异常
var nie = new NotImplementedException();//没有实现的异常
C#的I/O操作
打开文件的操作
1、File.Exists
2、Directory.Exists
3、string path=" “;
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files = dir.GetFiles(”*.exe");
写入文件的操作
不存在文件,创建文件写入数据
private const string FILE_NAME = "Test.txt";
static void Main(string[] args)
{
if(File.Exists(FILE_NAME))
{
Console.WriteLine("{0} already exists!",FILE_NAME);
Console.ReadLine();
return;
}
FileStream fs = new FileStream(FILE_NAME ,FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
for(int i=0;i<11;i++)
{
w.Write("a");
}
w.Close();
fs.Close();
}
5、存在文件,读取文件
private const string FILE_NAME ="Test.txt";
static void Main(string[] args)
{
if(!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist!",FILE_NAME);
Console.ReadLine();
return;
}
FileStream fs = new FileStream(FILE_NAME,FileMode.Open,FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
for(int i = 0; i<11 ; i++)
{
Console.WriteLine(r.ReadString());
}
r.Close();
fs.Close();
Console.ReadLine();
}
6、读文件
private const string FILE_NAME ="Test.txt";
static void Main(string[] args)
{
if(!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist!",FILE_NAME);
Console.ReadLine();
return;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
string input;
while((input = sr.ReadLine())!=null)
{
Console.WriteLine("The end of the steam");
sr.Close();
}
}
Console.ReadLine();
}
C#的Attribute
1、[Conditional(“DEBUG”)]
2、[Obsolete(“Don’t use old Method!”,true)]弃用了方法
[AttributeUsage(AttributeTargets.Class,AllowMultiple=false,Inherited=false)]
public class HelpAttribute : Attribute
{
protected string description;
public HelpAttribute(string description_in)
{
this.description = description_in;
}
public string Description
{
get
{
return this.description;
}
}
public string Name
{
get;
set;
}
}
//value type,System.Type
[Help("this is a do-nothing class",Name="极客学院")]
public class AnyClass
{
}
通过反射来获取Attribute中的信息
[Attribute]
static void Main(string[] args)
{
HelpAttribute helpAttribute;
foreach (var attr in typeof(AnyClass).GetCustomAttributes(true))
{
helpAttribute = attr as HelpAttribute;
if (null != helpAttribute)
{
Console.WriteLine("AnyClass Description:{0}", helpAttribute.Description);
Console.WriteLine(helpAttribute.Name);
}
}
}
C#反射
static void Main(string[] args)
{
string s = "Hello Reflection";
Type t = s.GetType(); //直接获取类型
Console.WriteLine(t.FullName);
Type t2 = Type.GetType("System.String",false,true); // 静态方法获取类型
Console.WriteLine(t2.FullName);
Type t3 = typeof(string); //
Console.WriteLine(t3.FullName);
Console.ReadLine();
}
反射获取方法
MethodInfo[ ] mi = t.GetMethods(BindingFlags.Public|BindingFlags.Instance);
反射获取字段
GetFileds()、GetProperites()
C#中动态加载以及推迟绑定
Assembly objAssembly = Assembly.GetExecutingAssembly();
Type t = objAssembly.GetType("反射实现动态加载.Car", false, true);
object obj = Activator.CreateInstance(t);
MethodInfo mi = t.GetMethod("IsMoving");
var isMoving = (bool)mi.Invoke(obj, null);
运行时创建新的类,然后动态加载进来
//System.Emit