注册 登录
编程论坛 C# 论坛

C#,使用vs 2015, 我想把各个项目都要用到的相同的类都放在一个共用.cs文件里集中管理,更新升级都能使用,该怎么做呢?

瓯旸臻骅 发布于 2021-03-10 16:34, 1240 次点击
RT!
求指教
1 回复
#2
qq28895779662021-04-15 11:35
使用动态编译
比如
程序代码:

CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
                string Output = "~Setup.exe";
                CompilerParameters parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add(@"System.dll");
                parameters.ReferencedAssemblies.Add(@"System.Windows.dll");
                parameters.ReferencedAssemblies.Add(@"System.Windows.Forms.dll");
                parameters.ReferencedAssemblies.Add(@"System.Xaml.dll");
                parameters.ReferencedAssemblies.Add(@"System.Xml.dll");
                parameters.ReferencedAssemblies.Add(@"System.Drawing.dll");
                parameters.ReferencedAssemblies.Add(@"WPFSystem.Windows.Presentation.dll");
                parameters.ReferencedAssemblies.Add(@"WPFPresentationCore.dll");
                parameters.ReferencedAssemblies.Add(@"WPFPresentationFramework.dll");
                parameters.ReferencedAssemblies.Add(@"WPFWindowsBase.dll");
                parameters.EmbeddedResources.Add("setup_res.resources");

                parameters.GenerateExecutable = true;
                = " -t:winexe";
                parameters.OutputAssembly = Output;

                CompilerResults results = (
                    parameters, 你的cs文件
                    );


比如建立一个wpf的以管理员运行的cs文件的字符串 code= @"下面的字符串"
程序代码:

string code = @"using System;
using System.Xml;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Documents;
using System.Resources;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Reflection;
using System.Diagnostics;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Threading;
using System.Runtime.Serialization.Formatters.Binary;
using partial class App:System.Windows.Application
{
    [STAThread]   
    public static void Main()
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
                    
        if (principal.IsInRole(WindowsBuiltInRole.Administrator))
        {
            App app=new App();
            app.Run();
        }
        else
        {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            startInfo.FileName = Assembly.GetExecutingAssembly().Location;
            startInfo.Verb = ""runas"";
            try
            {
                System.Diagnostics.Process.Start(startInfo);
            }
            catch
            {
                return;
            }
            System.Windows.Application.Current.Shutdown();
        }
    }

static Grid grid;
    static Window w;
    static Canvas mainBox;

protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        w=new Window();
        w.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        w.WindowStyle = WindowStyle.None;
        w.AllowsTransparency = true;
        w.Background = null;
        w.Width = 783;
        w.Height = 485;
        grid=new Grid();
        w.Content = grid;
        mainBox = new Canvas() { Width=783, Height=485,};
        grid.Children.Add(mainBox);
        w.Topmost = true;

        
        w.Show();
    }

1