I have a text (code.txt) file which inherit interface
public class TablePrinting : ITable
{
public void Table()
{
Console.WriteLine("This Is Table Method!!!!!");
Console.WriteLine("Dynamic Binding Is Successful !!!");
}
}
and i also write a code VS 2013
namespace DemoTablePrinting
{
class Program
{
interface ITable
{
void Table();
}
static void Main(string[] args)
{
string Content =File.ReadAllText(#"C:\Users\Asif\Desktop\code.txt");
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameter = new CompilerParameters();
parameter.ReferencedAssemblies.Add("System.dll");
parameter.GenerateInMemory = false;
parameter.GenerateExecutable = true;
CompilerResults result =provider.CompileAssemblyFromSource(parameter, Content);
if (result.Errors.Count > 0)
{
Console.WriteLine("Errors building\n\n {0} into {1} ",Content, result.PathToAssembly);
foreach (CompilerError ce in result.Errors)
{
Console.WriteLine(" {0}", ce.ToString());
Console.WriteLine();
}
}
else
{
Console.WriteLine("Source\n\r {0} built successfully.",Content);
}
Assembly assembly = result.CompiledAssembly;
Type type = assembly.GetType("hello.TablePrinting");
Object obj = Activator.CreateInstance(type);
}
}
}
but it gives a compile error and and execption ITable file not found
Now How Can i call all the the method of text file using interface
You've defined your interface ITable inside Program. If you do this, you'll need to refer to it as Program.ITable, or include Using Program in the file where you define 'TablePrinting'.
If ITable isn't specific to Program, you should move the interface outside of the class.
You can nest classes but it's usually only done when you want to imply that the inner object is linked to the outer one.
Related
Just looking for the solution where I want to add the c# script to the cache after compilation so that it can be accessed at any time when required.
Class which holds the cache details.
Public Class cCache
{
public string sSessionID{get;set;}
public string sName{get;set;}
public string sValue{get;set;}
}
C# script code used:
public static Run(string sName)
{
"Console.WriteLine(sName);"
}
When the button is clicked the method is called in the repository where it calls the another method with the script as the parameter to be complied and returns back the result in "MethodInfo" reflection. The script in the method info is invoked with parameter for the script and executed.
Code:
public string Button_CLick(string sScript)
{
MethodInfo methodInfo = WriteMethod(sScript);
cCache cChe= new cCache();
cChe.sSessionID="XYZ";
cChe.sName="Script";
cChe.sValue=MethodInfo.ToString();
if (methodInfo != null)
{
oResult = methodInfo.Invoke(null, new object[] { sName });
}
}
public MethodInfo WriteMethod(string sScript)
{
string sCode= #" using System;
namespace Scripting
{
public class AddScript
{
" + sScript + #"
}
}
";
CompilerParameters loParameters = new CompilerParameters();
loParameters.ReferencedAssemblies.Add("System.dll");
loParameters.GenerateInMemory = false;
ICodeCompiler provider = new CSharpCodeProvider().CreateCompiler();
CompilerResults results = provider.CompileAssemblyFromSource(loParameters, sCode);
if (results.Errors.HasErrors)
{
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in results.Errors)
{
sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
}
throw new InvalidOperationException(sb.ToString());
}
Type binaryFunction = results.CompiledAssembly.GetType("Scripting.AddScript");
return binaryFunction.GetMethod(Run);
}
The above code is working fine but before invoking the script method I want to add the complied script to the cache and then invoke or call it with parameter when ever required as the class property is string I am getting error when converting it to string, so please anyone can help me to sort this error.
I have class:
public class Domain
{
public static Assembly[] GetAssemblies()
{
var assemblies = new List<Assembly>();
foreach (ProcessModule module in Process.GetCurrentProcess().Modules)
{
try
{
var assemblyName = AssemblyLoadContext.GetAssemblyName(module.FileName);
var assembly = Assembly.Load(assemblyName);
assemblies.Add(assembly);
}
catch (BadImageFormatException)
{
// ignore native modules
}
}
return assemblies.ToArray();
}
}
My main class looks like:
class Program
{
public static Dictionary<String, Type> animals;
static void Main(string[] args)
{
var assTab = Domain.GetAssemblies();
foreach (var assembly in assTab)
{
var m = assembly.GetCustomAttribute<Method>();
if (m != null)
{
animals.Add(m.Name, assembly.GetType());
}
}
Where Method is a MethodAttribute class. In Animal.dll I have class like Dog, Cat etc. with Attribute [Method("cat")] and so on. To my dictionary I want to add this attribute name as string and type as Type (dog, Dog) and so on. My problem is that my program do not do that. After running program in variable animal I have 0 score. What should I change to achieve what I want?
The problem is in this line:
var m = assembly.GetCustomAttribute<Method>();
This line is getting attributes on the assembly. So if the attribute is not applied to the assembly then you will get null back.
What you need to do is get the types in the assembly first and then check each type for whether it has the attribute on it. Something more like this:
foreach(Type type in assembly.GetTypes())
{
var attr = assembly.GetCustomAttribute<Method>();
if (attr!=null)
{
animals.Add(attr.Name, type);
}
}
I need to enumerate all methods in a .NET module and check if they have tiny or fat header. I decided to use the powerful dnlib .NET modules handling library.
There is a dnlib.DotNet.Writer.MethodBody class that indicates the tiny/fat method header (see IsTiny() and IsFat()). However, I have no idea how to access this class.
The class is located in the Writer namespace - let's use the Writer Listener:
public void OnWriterEvent(ModuleWriterBase writer, ModuleWriterEvent evt)
{
Note that it's easy to enumerate all methods and get the other MethodBody, the one in Emit namespace:
foreach (TypeDef type in module.GetTypes())
{
foreach (MethodDef method in type.Methods)
{
dnlib.DotNet.Emit.MethodBody body = method.MethodBody;
}
}
}
Unfortunately this class doesn't reveal anything useful. I believe something similar to the following pseudocode should work:
public void OnWriterEvent(ModuleWriterBase writer, ModuleWriterEvent evt)
{
foreach (TypeDef type in module.?)
{
foreach (? method in type.?)
{
dnlib.DotNet.Writer.MethodBody body = method.?;
}
}
}
After fiddling with dnlib I figured that they don't really expose method headers, flags or raw method bytes. Using their own code I managed to read the header myself:
class MainClass
{
public static void TestMethod() {
return;
}
public static void Main (string[] args)
{
// Load mscorlib.dll
string filename = typeof(void).Module.FullyQualifiedName;
ModuleDefMD mod = ModuleDefMD.Load(Assembly.GetExecutingAssembly().Location);
foreach (TypeDef type in mod.GetTypes()) {
Console.WriteLine("Type: {0}", type.FullName);
foreach (var method in type.Methods) {
using (var reader = mod.MetaData.PEImage.CreateFullStream()) {
reader.Position = (long)mod.MetaData.PEImage.ToFileOffset(method.RVA);
var flags = reader.ReadByte ();
Console.WriteLine (" Method: {0}, Tiny: {1}", method.Name, (flags & 3) == 2);
}
}
}
Console.WriteLine();
}
}
I'm trying to figure out how to save a class object as an assembly, so that I can save that assembly to a local location etc to be used at at a later stage
Example the test class created, I'd like to create an assembly of it after in was instantiated.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
test newtest = new test("someInfo");
//Create assembly from tt
Assembly newAssem = NewAssembly.CreateAssembly(tt);
}
}
public class test
{
public string val {get ;set;}
public test(string v)
{
val = v;
}
}
}
So then in this method I want to create the assembly
public static class NewAssembly
{
public static Assembly CreateAssembly(test tt)
{
Assembly _proxyAssembly;
CodeDomProvider _codeDomProvider = CodeDomProvider.CreateProvider("CS");
CodeCompileUnit _codeCompileUnit = new CodeCompileUnit();
string _proxyCode;
using (StringWriter writer = new StringWriter())
{
CodeGeneratorOptions codeGenOptions = new CodeGeneratorOptions();
codeGenOptions.BracingStyle = "C";
_codeDomProvider.GenerateCodeFromCompileUnit(_codeCompileUnit, writer, codeGenOptions);
writer.Flush();
_proxyCode = writer.ToString();
}
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.OutputAssembly = "some local path \\aName.dll";
CompilerResults results;
results = codeDomProvider.CompileAssemblyFromSource(compilerParams,_proxyCode);
_proxyAssembly = results.CompiledAssembly;
return _proxyAssembly;
}
}
That was the best examples I've been able to get together but I'm not sure if its correct and also don't know what I'm missing like where the actual class gets added and how to finish the method.
Any help would be great, thanks
I have an application which embedes (via BuildAction: Embedded Resource) referenced assembly (called ClassLibrary1) inside itself and loads it on AppDomain.CurrentDomain.AssemblyResolve event.
Main assembly defines a class Class1:
public class Class1
{
public Class2 MyField { get; set; }
}
It has a property of type Class2 defined in ClassLibrary1.
Definition of Class2:
public class Class2
{
public int A { get; set; }
}
In the main method I`m creating a new XmlSerializer(typeof(Class1)):
static void Main()
{
SubscribeAssemblyResolver();
MainMethod();
}
private static void MainMethod()
{
XmlSerializer xs2 = new XmlSerializer(typeof(Class1));
Class1 cl = new Class1();
}
While executing a programm I get the following error:
Unable to generate a temporary class (result=1).
error CS0012: The type 'ClassLibrary1.Class2' is defined in an assembly that is not referenced. You must add a reference to assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c06f123f2868e8c8'.
error CS0266: Cannot implicitly convert type 'object' to 'ClassLibrary1.Class2'. An explicit conversion exists (are you missing a cast?)
Any ideas?
The rest of the code:
private static void SubscribeAssemblyResolver()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
static Dictionary<String, Assembly> _assemblies = new Dictionary<String, Assembly>(StringComparer.OrdinalIgnoreCase);
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
return ResolveAssembly(args.Name);
}
private static Assembly ResolveAssembly(string argsName)
{
Assembly dll;
var name = "WindowsFormsApplication1.Libs." + new AssemblyName(argsName).Name + ".dll";
if (!_assemblies.TryGetValue(name, out dll))
{
Assembly res = typeof(Program).Assembly;
using (var input = res.GetManifestResourceStream(name))
{
if (input == null)
{
//TODO: log
return null;
}
Byte[] assemblyData = new Byte[input.Length];
input.Read(assemblyData, 0, assemblyData.Length);
if (null == (dll = Assembly.Load(assemblyData)))
{
//TODO: log
return null;
}
//TODO: log
_assemblies[name] = dll;
return dll;
}
}
return dll;
}
UPDATE: Created a BUG on the microsoft Connect site. You can also download a sample visual stuido 2010 solution (just expand Details fieldgroup) from there to reproduce it.
I've solved similar problem by saving assembly in temporary folder
public static byte[] ReadFully(Stream input)
{
var buffer = new byte[16 * 1024];
using (var ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
public App()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
var assemblyName = new AssemblyName(args.Name);
if (assemblyName.Name != "Omikad.Core")
return null;
var resourceName = "Terem." + assemblyName.Name + ".dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
if (stream == null)
return null;
var assemblyData = ReadFully(stream);
var tmp = Path.Combine(Path.GetTempPath(), "Omikad.Core.dll");
File.WriteAllBytes(tmp, assemblyData);
return Assembly.LoadFrom(tmp);
}
};
}
Try to add atribute:
[XmlInclude(typeof(Class2))]
public class Class1
{
public Class2 MyField { get; set; }
}
As for now I`ve ended up with two somewhat bad solutions:
While you can`t instanciate XmlSerializer for the type Class1, you still can instanciate it for the type Class2 from the main assembly. That does mean that if you move Class1 to ClassLibrary1 or Class2 to the main assembly - it will deserialize without errors. It works, but it is not possible to use this solution everywhere, plus it is ideologically wrong.
Use ILMerge to merge those assemblies into one. But it only works for non-wpf stuff, plus you should manage the situation with the assemblies attributes (there could be conflicts).
And one very bad idea:
Generate ClassLibrary1.XmlSerializer.dll with sgen.exe.
Also embed it into the main assembly.
Explicitly load it to the XmlSerializer cache calling one of it`s internal methods via reflection.
Although I had to use solution number one for now, I`m not satisfied with it, because it is too constraining.
I'd try the XmlSerializer(Type, Type[]) constructor and provide Class2 as an additional type using the second parameter. I've few experience with the XmlSerializer, but for DataContractSerializer this does the trick.