0

I wonder to know if it's possible. For example bootstrap has new version and i need to change cdn url and re-build project for RegisterBundles in asp.net web forms. It's simple url that we don't need to re-build project for that issue.

Is it possible that reading a *.txt file and using as c# code in a class. Class will be same as before and we will survive.

Example RegisterBundles code:

public class stylescriptbundle
{
   public void RegisterBundles(BundleCollection bundles)
   {
      BundleTable.Bundles.Add(new ScriptBundle("/mybundle").Include(
         "https://code.jquery.com/jquery-3.4.1.slim.min.js",
         "https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js",
        "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" 
      );
      BundleTable.Bundles.UseCdn = true;
   }
}
Hallowen
  • 133
  • 2
  • 14
  • i can read/write to txt file and showing in a page but i am not sure how can i call inside a class those strings as a c# code. – Hallowen Dec 08 '19 at 16:32

2 Answers2

2

First, similar to what robbpriestley said, what you should do is read the strings from a file so you can include them in the bundle. Configuration file is best, but if you're hellbent on a .txt, try below:

var scripts = File.ReadAllLines("someConfigurationFile.txt");
var bundle = new ScriptBundle("/myBundle");
foreach(var script in scripts)
{
    bundle.Include(script);
}
BundleTable.Bundles.UseCdn = true;
BundleTable.Bundles.Add(bundle);

(the above is just off the top of my head, you may have to tweak it to get it to work.)

That said, if you still want to compile code at run time, you can use the Microsoft.CSharp and Microsoft.CodeDom.Compiler namespaces, according to this tutorial. I'll try and summarize it here, for archival reasons.

  1. Get your code into a string: var codeStr = File.ReadAllText("runtime compiled.cs");
  2. Create a provider and compiler: var provider = new CSharpCodeProvider(); var parameters = new CodeParameters();
  3. Define parameters of the compiler. It sounds like you'll definitely want to compile it into memory, and you'll need to reference any assemblies you use in that code: parameters.GenerateInMemory = true; parameters.ReferencedAssemblies.Add("necessaryAssembly.dll");
  4. compile: var results = provider.CompileAssemblyFromSource(parameters, code);
  5. Check errors
  6. Get your assembly: var assembly = results.CompiledAssembly; so you can get your type: var program = assembly.GetType("first.program"); so you can get your method: var main = program.GetMethod("Main");
  7. And then you can call your method with Invoke: main.Invoke(null, null); (check out reference on MethodInfo.)
Adam R. Grey
  • 1,861
  • 17
  • 30
  • 1
    This information is very helpful to understand situation. I just don't want to inflate configuration file. That is why i am looking for fast and easy editable possibility. Thanks, – Hallowen Dec 08 '19 at 17:16
1

Don't use a TXT file for this. Put the strings as settings into your web.config and then use the provided ASP.NET ConfigurationManager to reference them.

For example, see: https://stackoverflow.com/a/12892083/1348592

robbpriestley
  • 3,050
  • 2
  • 24
  • 36