从代码visual studio生成类

时间:2014-04-01 01:10:01

标签: class generator

有没有办法可以根据我的应用程序中的现有代码生成一个类?

也就是说,我编写的代码不使用类,我希望将其合并到我的项目中。

我可以手动完成,但我想知道Visyal Studio中是否有一个像折射器这样的函数可以用来选择代码范围并将其转换为类。

谢谢!

编辑:

我们假设:

    FolderBrowserDialog directorio = new FolderBrowserDialog();
    directorio.ShowNewFolderButton = false;
    directorio.SelectedPath = @"C:\TRACKER\DICOM";
    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;
    }

1 个答案:

答案 0 :(得分:1)

只需将代码放入新类的方法中......

public class MyClass
{
    public bool DoSomething()
    {
        FolderBrowserDialog directorio = new FolderBrowserDialog();
        directorio.ShowNewFolderButton = false;
        directorio.SelectedPath = @"C:\TRACKER\DICOM";
        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();