using System.Windows;
using System;
using System.Reflection;
namespace test7
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
eslre c = new eslre();
Type myType = typeof(string); //静态获取Form的数据类型
PropertyInfo[] pInfos = myType.GetProperties(); //动态获取pInfos的所有属性
MethodInfo[] mInfos = myType.GetMethods(); //动态获取mInfos的所有方法
foreach (var p in pInfos) //遍历打印
{
Console.WriteLine(p.Name);
}
Console.WriteLine("---------------------------------------------");
foreach (var m in mInfos)
{
Console.WriteLine(m.Name);
}
Console.WriteLine("---------------------------------------------");
Console.WriteLine(myType.FullName);
}
}
class eslre
{
//public void PrintXTo1(int x)//遍历打印
//{
// for (int i = x; i>=0; i--)
// {
// Console.WriteLine(i);
// }
//}
//public void PrintXTo2(int x)//递归打印
//{
// if (x==1)
// {
// Console.WriteLine(x);
// }
// else
// {
// Console.WriteLine(x);
// PrintXTo2(x - 1);
// }
//}
}
}
二内存管理-栈溢出
using System.Windows;
using System;
using System.Reflection;
namespace test7
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
eslre c = new eslre();
c.BadMethod();
}
}
class eslre
{
public void BadMethod() //递归栈溢出,每次调用BadMethod都要分配内存给x
{
int x = 100;
this.BadMethod();
}
}
class Program//这代码得把前面的程序都删了
{
static unsafe void Main(string[] args)//加unsafe告诉编译器在编写不安全的程序,不然报错
{
int* p = stackalloc int[99999999];//通过指针分配超出栈空间的内存,导致栈溢出
}
}
}
三堆溢出
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace test8
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<Window> winList;//定义了一个 List<Window> 类型的字段,用来存放 Window 对象的集合,未实例化
private void Button_Click_1(object sender, RoutedEventArgs e)
{
winList = new List<Window>();
for(int i = 0; i < 15000; i++)
{
Window w = new Window();//创建一个新的window
winList.Add(w);//把新的window加进winList集合中
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
winList.Clear();//清空winList集合,垃圾回收器会在合适的时候释放对应的内存,不是立刻
}
}
}