005days
反射异常处理正则表达式字符串类
反射
using System;
using System.Reflection;
class Person
{
public int a;
private int b;
public static int c;
private static int d;
public Person()
{
Console.WriteLine("Person类无参的构造方法被调用");
}
private Person(int a,double b )
{
Console.WriteLine("有参构造函数被调用了");
}
public void ShowA() { }
private void ShowB() { }
public static void ShowV() { }
private static void ShowD() { }
public int show(int a,double b)
{
return a;
}
private int show(double a, int b)
{
return b;
}
}
class Program
{
public static void Main(string[] args)
{
Type t = Type.GetType("Person");
object obj = Activator.CreateInstance(t, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { 1, 3.1 }, null);
FieldInfo a = t.GetField("a");
a.SetValue(obj, 1);
Object aa = a.GetValue(obj);
FieldInfo b = t.GetField("b", BindingFlags.NonPublic | BindingFlags.Instance);
b.SetValue(obj, 2);
object bb = b.GetValue(obj);
FieldInfo c = t.GetField("c", BindingFlags.Public | BindingFlags.Static);
c.SetValue(null, 2);
object cc = c.GetValue(null);
FieldInfo d = t.GetField("d", BindingFlags.NonPublic | BindingFlags.Static);
d.SetValue(null, 2);
object dd =d.GetValue(null);
MethodInfo method0= t.GetMethod("ShowD", BindingFlags.NonPublic | BindingFlags.Static);
method0.Invoke(null, null);
MethodInfo method1 = t.GetMethod("show", BindingFlags.Public | BindingFlags.Instance, null, new Type[] {typeof(int), typeof(double) }, null);
object result = method1.Invoke(obj, new object[] { 1, 3.13 });
Console.WriteLine(result);
}
}
异常处理
using System;
namespace 异常处理
{
class Program
{
static void Main(string[] args)
{
try
{
int[] ary = new int[3];
ary[4] = 0;
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("发生了数组越界的异常");
}
catch (DivideByZeroException)
{
Console.WriteLine("除0异常");
}
catch (Exception)
{
Console.WriteLine("所有的异常");
}
Console.WriteLine("程序结束");
try
{
person.ScoreException();
}
catch (ScoreException exception) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(exception);
}
}
}
class ScoreException : Exception {
public ScoreException(string message) : base(message) { }
}
class person
{
public static void ScoreException()
{
ScoreException exception = new ScoreException("这个是自定义的异常");
throw exception;
}
}
}
正则表达式
using System;
using System.Text.RegularExpressions;
namespace housebook6
{
class Program
{
static void Main(string[] args)
{
Regex r = new Regex("^hello world$");
Regex r1 = new Regex("^[abcdefg]ello world$");
Regex r2 = new Regex("^[1-9]ello world$");
bool result = r.IsMatch("hello world");
bool result1 = r1.IsMatch("hello world");
bool result2 = r2.IsMatch("0ello world");
Console.WriteLine(result);
Console.WriteLine(result1);
Console.WriteLine(result2);
string names = "laowang xiaomin uncle wang";
string pattern = " {2,}";
Console.WriteLine(Regex.Replace(names,pattern,", "));
Console.WriteLine( CheckPhone("18888888888"));
}
public static string CheckPhone(string phone) {
string pattern1 = @"^(1[34578]\d)(\d{4})(\d{4})$";
Match match = Regex.Match(phone, pattern1);
GroupCollection groups = match.Groups;
string group1 = groups[1].Value;
string group2 = groups[2].Value;
string group3 = groups[3].Value;
return group1 + "****" + group3;
}
}
}
字符串类
using System;
namespace 字符串类
{
class Program
{
static void Main(string[] args)
{
char[] s = { 'h', 'e', 'l', 'l', 'o' };
string str = new string(s);
int result = "hello".CompareTo("hellloo");
Console.WriteLine(result);
bool c = "hello world".Contains("llo");
bool d = "harry".EndsWith("ry");
Console.WriteLine(d);
string result1 = "ello".Insert(0, "h");
Console.WriteLine(result1);
string[] result2 = "xiaoming,laowang,xiaonghong".Split(',');
}
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-65016.html