有没有办法根据应用程序上的现有代码生成类?
也就是说,我编写的代码不使用类,我想将其合并到我的项目中。
我可以手动完成,但我想知道 Visyal Studio 中是否有折射器等函数可用于选择代码绑定并将其转换为类。
谢谢!
编辑:
让我们假设:
FolderBrowserDialog directorio = new FolderBrowserDialog();
directorio.ShowNewFolderButton = false;
directorio.SelectedPath = @"C:TRACKERDICOM";
directorio.Description = "Select a directory with Dicom Images";
if (directorio.ShowDialog() == DialogResult.OK)
{
path_directorio_imagenes = directorio.SelectedPath;
FileInfo[] fileInfo = new DirectoryInfo(path_directorio_imagenes).GetFiles();
if (fileInfo.Length == 0)
{
MessageBox.Show("The directory doesn't contains any file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
int nro_archivos = fileInfo.Length;
for (int i = 0; i < nro_archivos; ++i)
{
string archivo = fileInfo[i].FullName;
BinaryReader br = new BinaryReader(new FileStream(archivo, FileMode.Open, FileAccess.Read), Encoding.ASCII);
byte[] preamble = new byte[132];
br.Read(preamble, 0, 132);
if (preamble[128] != 'D' || preamble[129] != 'I' || preamble[130] != 'C' || preamble[131] != 'M')
{
MessageBox.Show("The directory contains a file that's not a DICOM file");
return false;
}
}
return true;
}
else
{
return false;
}
只需将代码放入新类的方法中即可...
public class MyClass
{
public bool DoSomething()
{
FolderBrowserDialog directorio = new FolderBrowserDialog();
directorio.ShowNewFolderButton = false;
directorio.SelectedPath = @"C:TRACKERDICOM";
directorio.Description = "Select a directory with Dicom Images";
if (directorio.ShowDialog() == DialogResult.OK)
{
path_directorio_imagenes = directorio.SelectedPath;
FileInfo[] fileInfo = new DirectoryInfo(path_directorio_imagenes).GetFiles();
if (fileInfo.Length == 0)
{
MessageBox.Show("The directory doesn't contains any file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
int nro_archivos = fileInfo.Length;
for (int i = 0; i < nro_archivos; ++i)
{
string archivo = fileInfo[i].FullName;
BinaryReader br = new BinaryReader(new FileStream(archivo, FileMode.Open, FileAccess.Read), Encoding.ASCII);
byte[] preamble = new byte[132];
br.Read(preamble, 0, 132);
if (preamble[128] != 'D' || preamble[129] != 'I' || preamble[130] != 'C' || preamble[131] != 'M')
{
MessageBox.Show("The directory contains a file that's not a DICOM file");
return false;
}
}
return true;
}
else
{
return false;
}
}
}
然后当你需要使用它时。
MyClass myClass = new MyClass();
bool result = myClass.DoSomething();