I am trying to merge the IL of two methods: one method is from an already existing assembly (MyAssembly.dll) and one is in the same project as my dnlib code. I'm trying to read both methods, merge their instructions and then write it to a new assembly. This works, however when I add if statements (probably also other statements) to the new method (so the one from my project) I get an exception when writing the instructions to the new assembly:
Unhandled Exception: dnlib.DotNet.Writer.ModuleWriterException: Found some other method's instruction or a removed instruction. You probably removed an instruction that is the target of a branch instruction or an instruction that's the first/last inst
ruction in an exception handler.
at dnlib.DotNet.DummyLogger.Log(Object sender, LoggerEvent loggerEvent, String format, Object[] args)
at dnlib.DotNet.Writer.ModuleWriterBase.dnlib.DotNet.ILogger.Log(Object sender, LoggerEvent loggerEvent, String format, Object[] args)
at dnlib.DotNet.Writer.MetaData.Error(String message, Object[] args)
at dnlib.DotNet.Writer.MetaData.dnlib.DotNet.Writer.IWriterError.Error(String message)
at dnlib.DotNet.Writer.MethodBodyWriter.ErrorImpl(String message)
at dnlib.DotNet.Writer.MethodBodyWriterBase.Error(String message)
at dnlib.DotNet.Writer.MethodBodyWriterBase.GetOffset(Instruction instr)
at dnlib.DotNet.Writer.MethodBodyWriterBase.WriteShortInlineBrTarget(BinaryWriter writer, Instruction instr)
at dnlib.DotNet.Writer.MethodBodyWriterBase.WriteOperand(BinaryWriter writer, Instruction instr)
at dnlib.DotNet.Writer.MethodBodyWriterBase.WriteInstruction(BinaryWriter writer, Instruction instr)
at dnlib.DotNet.Writer.MethodBodyWriterBase.WriteInstructions(BinaryWriter writer)
at dnlib.DotNet.Writer.MethodBodyWriter.WriteFatHeader()
at dnlib.DotNet.Writer.MethodBodyWriter.Write()
at dnlib.DotNet.Writer.MetaData.WriteMethodBodies()
at dnlib.DotNet.Writer.MetaData.Create()
at dnlib.DotNet.Writer.MetaData.CreateTables()
at dnlib.DotNet.Writer.ModuleWriter.WriteImpl()
at dnlib.DotNet.Writer.ModuleWriterBase.Write(Stream dest)
at dnlib.DotNet.Writer.ModuleWriterBase.Write(String fileName)
at dnlib.DotNet.ModuleDef.Write(String filename, ModuleWriterOptions options)
at dnlib.DotNet.ModuleDef.Write(String filename)
It does not throw an exception when the original code (the code from MyAssembly.dll) contains an if statement.
MyAssembly.dll's code:
string str = GameManager.m_GameVersionString;
if (GameManager.m_Changelist != string.Empty)
{
str = str + " (" + GameManager.m_Changelist + ")";
}
return str + " Release ";
MyAssembly.dll's IL:
IL_0000: ldsfld System.String GameManager::m_GameVersionString
IL_0005: stloc.0
IL_0006: ldsfld System.String GameManager::m_Changelist
IL_000B: ldsfld System.String System.String::Empty
IL_0010: call System.Boolean System.String::op_Inequality(System.String,System.String)
IL_0015: brfalse IL_0030
IL_001A: ldloc.0
IL_001B: ldstr " ("
IL_0020: ldsfld System.String GameManager::m_Changelist
IL_0025: ldstr ")"
IL_002A: call System.String System.String::Concat(System.String,System.String,System.String,System.String)
IL_002F: stloc.0
IL_0030: ldloc.0
IL_0031: ldstr " Release "
IL_0036: call System.String System.String::Concat(System.String,System.String)
IL_003B: stloc.0
IL_003C: ldloc.0
IL_003D: ret
My project's code:
string check = "Hello".Trim();
if (check == "Hello")
{
Console.WriteLine("Hello");
}
My project's IL
IL_0000: ldstr "Hello"
IL_0005: call System.String System.String::Trim()
IL_000A: ldstr "Hello"
IL_000F: call System.Boolean System.String::op_Equality(System.String,System.String)
IL_0014: brfalse.s IL_0020
IL_0016: ldstr "Hello"
IL_001B: call System.Void System.Console::WriteLine(System.String)
IL_0020: ret
Merged IL
IL_0000: ldstr "Hello"
IL_0005: call System.String System.String::Trim()
IL_000A: ldstr "Hello"
IL_000F: call System.Boolean System.String::op_Equality(System.String,System.String)
IL_0014: brfalse.s IL_0020
IL_0016: ldstr "Hello"
IL_001B: call System.Void System.Console::WriteLine(System.String)
IL_0020: ldsfld System.String GameManager::m_GameVersionString
IL_0025: stloc.0
IL_0026: ldsfld System.String GameManager::m_Changelist
IL_002B: ldsfld System.String System.String::Empty
IL_0030: call System.Boolean System.String::op_Inequality(System.String,System.String)
IL_0035: brfalse.s IL_004D
IL_0037: ldloc.0
IL_0038: ldstr " ("
IL_003D: ldsfld System.String GameManager::m_Changelist
IL_0042: ldstr ")"
IL_0047: call System.String System.String::Concat(System.String,System.String,System.String,System.String)
IL_004C: stloc.0
IL_004D: ldloc.0
IL_004E: ldstr " Release "
IL_0053: call System.String System.String::Concat(System.String,System.String)
IL_0058: stloc.0
IL_0059: ldloc.0
IL_005A: ret
The merged IL looks identical to the IL I generated with dnSpy: https://owo.whats-th.is/a0783e.png
However, an error is thrown when writing the merged IL to a file. My code:
//Patch method
...
var patchModule = ModuleDefMD.Load(patchClass.Module);
var patchMethod = FindMethod(patchModule, patchClass, method.Name);
var assemblyModule = ModuleDefMD.Load(assemblyPath);
var assemblyMethod = FindMethod(assemblyModule, attribute.Type, attribute.MethodName, attribute.Parameters);
assemblyMethod.Body = MergeMethods(patchMethod, assemblyMethod, attribute.CodeMode, attribute.CustomPos);
assemblyModule.Write(finalPath); //Error is thrown here
...
private static CilBody MergeMethods(MethodDef original, MethodDef target, AmityPatch.Mode mode, int mergeLoc = 0)
{
original.FreeMethodBody();
target.FreeMethodBody();
var originalBody = original.Body;
var targetBody = target.Body;
var targetModule = target.Module;
var originalInstructions = originalBody.Instructions;
var targetInstructions = targetBody.Instructions;
Console.WriteLine("=original method=");
Console.WriteLine();
foreach (var originalInstruction in originalInstructions)
{
Console.WriteLine(originalInstruction);
}
Console.WriteLine();
Console.WriteLine("=target method=");
Console.WriteLine();
foreach (var targetInstruction in targetInstructions)
{
Console.WriteLine(targetInstruction);
}
RemoveReturn(ref originalInstructions, true);
if (mode == AmityPatch.Mode.Postfix) mergeLoc = targetInstructions.Count - 1;
var localOffset = targetBody.Variables.Count;
for (var i = 0; i < originalBody.Variables.Count; i++)
{
targetBody.Variables.Add(
new Local(originalBody.Variables[i].Type, originalBody.Variables[i].Name));
}
for (var i = originalInstructions.Count - 1; i >= 0; i--)
{
var o = originalInstructions[i];
var c = new Instruction(o.OpCode, o.Operand);
switch (o.Operand)
{
case IType type:
c.Operand = targetModule.Import(type);
break;
case IMethod method:
c.Operand = targetModule.Import(method);
break;
case IField field:
c.Operand = targetModule.Import(field);
break;
}
if (IsStloc(o.OpCode))
{
c.OpCode = OpCodes.Stloc;
c.Operand = targetBody.Variables[StlocIndex(o) + localOffset];
}
else if (IsLdloc(o.OpCode))
{
c.OpCode = OpCodes.Ldloc;
c.Operand = targetBody.Variables[LdlocIndex(o) + localOffset];
}
else if (IsLdloca(o.OpCode))
{
c.OpCode = OpCodes.Ldloca;
c.Operand = targetBody.Variables[LdlocIndex(o) + localOffset];
}
targetInstructions.Insert(mergeLoc, c);
}
targetBody.OptimizeMacros();
targetBody.OptimizeBranches();
Console.WriteLine();
Console.WriteLine("=merged method=");
Console.WriteLine();
foreach (var instruction in targetBody.Instructions)
{
Console.WriteLine(instruction);
}
Console.WriteLine(targetBody.Variables.Count);
return targetBody;
}
I don't know what causes this issue. All labels in the IL seem to be correct. When removing the if statement dnlib writes the IL to the assembly just fine.
If the method to be merged in is from a different assembly, tokens may need to be created/converted.
Related
I am trying to print out the contents of an array after invoking some methods which alter it, in Java I use:
System.out.print(Arrays.toString(alg.id));
how do I do this in c#?
You may try this:
foreach(var item in yourArray)
{
Console.WriteLine(item.ToString());
}
Also you may want to try something like this:
yourArray.ToList().ForEach(i => Console.WriteLine(i.ToString()));
EDIT: to get output in one line [based on your comment]:
Console.WriteLine("[{0}]", string.Join(", ", yourArray));
//output style: [8, 1, 8, 8, 4, 8, 6, 8, 8, 8]
EDIT(2019): As it is mentioned in other answers it is better to use Array.ForEach<T> method and there is no need to do the ToList step.
Array.ForEach(yourArray, Console.WriteLine);
There are many ways to do it, the other answers are good, here's an alternative:
Console.WriteLine(string.Join("\n", myArrayOfObjects));
The easiest one e.g. if you have a string array declared like this string[] myStringArray = new string[];
Console.WriteLine("Array : ");
Console.WriteLine("[{0}]", string.Join(", ", myStringArray));
I decided to test the speeds of the different methods posted here:
These are the four methods I used.
static void Print1(string[] toPrint)
{
foreach(string s in toPrint)
{
Console.Write(s);
}
}
static void Print2(string[] toPrint)
{
toPrint.ToList().ForEach(Console.Write);
}
static void Print3(string[] toPrint)
{
Console.WriteLine(string.Join("", toPrint));
}
static void Print4(string[] toPrint)
{
Array.ForEach(toPrint, Console.Write);
}
The results are as follows:
Strings per trial: 10000
Number of Trials: 100
Total Time Taken to complete: 00:01:20.5004836
Print1 Average: 484.37ms
Print2 Average: 246.29ms
Print3 Average: 70.57ms
Print4 Average: 233.81ms
So Print3 is the fastest, because it only has one call to the Console.WriteLine which seems to be the main bottleneck for the speed of printing out an array. Print4 is slightly faster than Print2 and Print1 is the slowest of them all.
I think that Print4 is probably the most versatile of the 4 I tested, even though Print3 is faster.
If I made any errors, feel free to let me know / fix them on your own!
EDIT: I'm adding the generated IL below
g__Print10_0://Print1
IL_0000: ldarg.0
IL_0001: stloc.0
IL_0002: ldc.i4.0
IL_0003: stloc.1
IL_0004: br.s IL_0012
IL_0006: ldloc.0
IL_0007: ldloc.1
IL_0008: ldelem.ref
IL_0009: call System.Console.Write
IL_000E: ldloc.1
IL_000F: ldc.i4.1
IL_0010: add
IL_0011: stloc.1
IL_0012: ldloc.1
IL_0013: ldloc.0
IL_0014: ldlen
IL_0015: conv.i4
IL_0016: blt.s IL_0006
IL_0018: ret
g__Print20_1://Print2
IL_0000: ldarg.0
IL_0001: call System.Linq.Enumerable.ToList<String>
IL_0006: ldnull
IL_0007: ldftn System.Console.Write
IL_000D: newobj System.Action<System.String>..ctor
IL_0012: callvirt System.Collections.Generic.List<System.String>.ForEach
IL_0017: ret
g__Print30_2://Print3
IL_0000: ldstr ""
IL_0005: ldarg.0
IL_0006: call System.String.Join
IL_000B: call System.Console.WriteLine
IL_0010: ret
g__Print40_3://Print4
IL_0000: ldarg.0
IL_0001: ldnull
IL_0002: ldftn System.Console.Write
IL_0008: newobj System.Action<System.String>..ctor
IL_000D: call System.Array.ForEach<String>
IL_0012: ret
Another approach with the Array.ForEach<T> Method (T[], Action<T>) method of the Array class
Array.ForEach(myArray, Console.WriteLine);
That takes only one iteration compared to array.ToList().ForEach(Console.WriteLine) which takes two iterations and creates internally a second array for the List (double iteration runtime and double memory consumtion)
Starting from C# 6.0, when $ - string interpolation was introduced, there is one more way:
var array = new[] { "A", "B", "C" };
Console.WriteLine($"{string.Join(", ", array)}");
//output
A, B, C
Concatenation could be archived using the System.Linq, convert the string[] to char[] and print as a string:
var array = new[] { "A", "B", "C" };
Console.WriteLine($"{new String(array.SelectMany(_ => _).ToArray())}");
//output
ABC
In C# you can loop through the array printing each element. Note that System.Object defines a method ToString(). Any given type that derives from System.Object() can override that.
Returns a string that represents the current object.
http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx
By default the full type name of the object will be printed, though many built-in types override that default to print a more meaningful result. You can override ToString() in your own objects to provide meaningful output.
foreach (var item in myArray)
{
Console.WriteLine(item.ToString()); // Assumes a console application
}
If you had your own class Foo, you could override ToString() like:
public class Foo
{
public override string ToString()
{
return "This is a formatted specific for the class Foo.";
}
}
If you want to get cute, you could write an extension method that wrote an IEnumerable<object> sequence to the console. This will work with enumerables of any type, because IEnumerable<T> is covariant on T:
using System;
using System.Collections.Generic;
namespace Demo
{
internal static class Program
{
private static void Main(string[] args)
{
string[] array = new []{"One", "Two", "Three", "Four"};
array.Print();
Console.WriteLine();
object[] objArray = new object[] {"One", 2, 3.3, TimeSpan.FromDays(4), '5', 6.6f, 7.7m};
objArray.Print();
}
}
public static class MyEnumerableExt
{
public static void Print(this IEnumerable<object> #this)
{
foreach (var obj in #this)
Console.WriteLine(obj);
}
}
}
(I don't think you'd use this other than in test code.)
I upvoted the extension method answer by Matthew Watson, but if you're migrating/visiting coming from Python, you may find such a method useful:
class Utils
{
static void dump<T>(IEnumerable<T> list, string glue="\n")
{
Console.WriteLine(string.Join(glue, list.Select(x => x.ToString())));
}
}
-> this will print any collection using the separator provided. It's quite limited (nested collections?).
For a script (i.e. a C# console application which only contains Program.cs, and most things happen in Program.Main) - this may be just fine.
this is the easiest way that you could print the String by using array!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace arraypracticeforstring
{
class Program
{
static void Main(string[] args)
{
string[] arr = new string[3] { "Snehal", "Janki", "Thakkar" };
foreach (string item in arr)
{
Console.WriteLine(item.ToString());
}
Console.ReadLine();
}
}
}
If it's an array of strings you can use Aggregate
var array = new string[] { "A", "B", "C", "D"};
Console.WriteLine(array.Aggregate((result, next) => $"{result}, {next}")); // A, B, C, D
that way you can reverses the order by changing the order of the parameters like so
Console.WriteLine(array.Aggregate((result, next) => $"{next}, {result}")); // D, C, B, A
You can use for loop
int[] random_numbers = {10, 30, 44, 21, 51, 21, 61, 24, 14}
int array_length = random_numbers.Length;
for (int i = 0; i < array_length; i++){
if(i == array_length - 1){
Console.Write($"{random_numbers[i]}\n");
} else{
Console.Write($"{random_numbers[i]}, ");
}
}
If you do not want to use the Array function.
public class GArray
{
int[] mainArray;
int index;
int i = 0;
public GArray()
{
index = 0;
mainArray = new int[4];
}
public void add(int addValue)
{
if (index == mainArray.Length)
{
int newSize = index * 2;
int[] temp = new int[newSize];
for (int i = 0; i < mainArray.Length; i++)
{
temp[i] = mainArray[i];
}
mainArray = temp;
}
mainArray[index] = addValue;
index++;
}
public void print()
{
for (int i = 0; i < index; i++)
{
Console.WriteLine(mainArray[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
GArray myArray = new GArray();
myArray.add(1);
myArray.add(2);
myArray.add(3);
myArray.add(4);
myArray.add(5);
myArray.add(6);
myArray.print();
Console.ReadKey();
}
}
In a DynamicMethod I try to call a method that wants an array of objects to return the length of the given array. Currently, my method which should be called from the DynamicMethod looks like the following:
public static int Test(Object[] args)
{
Console.WriteLine(args.Length);
return args.Length;
}
The creation process of the DynamicMethod looks like the following:
(The creation of the Object array is adopted from the following SO answer)
public static DynamicMethod GetDM()
{
var returnType = typeof(int);
var paramTypes = new Type[]{typeof(string), typeof(bool)};
var method = new DynamicMethod(
"",
returnType,
paramTypes,
false
);
var il = method.GetILGenerator();
// Save parameters in an object array
il.Emit(OpCodes.Ldc_I4_S, paramTypes.Length);
il.Emit(OpCodes.Newarr, typeof(Object));
il.Emit(OpCodes.Dup);
for (int i = 0; i < paramTypes.Length; i++)
{
Type type = paramTypes[i];
il.Emit(OpCodes.Ldc_I4, i);
il.Emit(OpCodes.Ldarg, i);
if (type.IsValueType) { il.Emit(OpCodes.Box, type); }
il.Emit(OpCodes.Stelem_Ref);
il.Emit(OpCodes.Dup);
}
// Call method and get the length of the array
// How do I return the result of the called method?
var callMethod = typeof(Program).GetMethod("Test", (BindingFlags)(-1));
il.Emit(OpCodes.Call, callMethod);
il.Emit(OpCodes.Ret);
return method;
}
With the following method I check the functionality:
public static void Main(string[] args)
{
var method = GetDM();
var result = method.Invoke(null, new Object[]{"Test 1234", true});
Console.WriteLine(result); // Should be 2
}
When I run the main method I get the System.Reflection.TargetInvocationException. Can someone help me out how to return the value which was returned by the called method? Here is a link to a dotnetfiddle to see my problem in action.
After the for loop you have the constructed object array twice on the stack (because of the Dup calls). Only one of these array references is consumed by the Call so at the end of the method you will have one extra array reference on the stack.
To correct this, remove the first Dup and move the second to the head of the loop body:
public static DynamicMethod GetDM() {
var returnType = typeof(int);
var paramTypes = new Type[]{typeof(string), typeof(bool)};
var method = new DynamicMethod(
"",
returnType,
paramTypes,
false
);
var il = method.GetILGenerator();
// Save parameters in an object array
il.Emit(OpCodes.Ldc_I4_S, paramTypes.Length);
il.Emit(OpCodes.Newarr, typeof(Object));
for (int i = 0; i < paramTypes.Length; i++)
{
Type type = paramTypes[i];
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Ldc_I4, i);
il.Emit(OpCodes.Ldarg, i);
if (type.IsValueType) { il.Emit(OpCodes.Box, type); }
il.Emit(OpCodes.Stelem_Ref);
}
// Call method and get the length of the array
// How do I return the result of the called method?
var callMethod = typeof(Program).GetMethod("Test", (BindingFlags)(-1));
il.Emit(OpCodes.Call, callMethod);
il.Emit(OpCodes.Ret);
return method;
}
I have a small read-only database (based on data from services). The load time is critical so data can't be extracted from DB, XML and so on. I use the direct construction of data in the generated C# file:
void Load(){
var aa = new A[ 100500 ];
aa[0] = new A( ... );
...
aa[100499] = new A( ... );
AA = aa;
}
There are no recursion here, stackallocks and so on.
But I have got a StackOverflow error.
Looking in Disassembler window I found that JIT converts this code into:
var aa = new A[ 100500 ];
var a0 = new A( ... );
aa[0] = a0;
...
var a100499 = new A( ... );
aa[100499] = a100499;
So it create 100500 variables aXXXXX in stack:
-- aa[32] = new A( qwerty, asdf );
mov ecx,185C10h -- var a32 = new A
call 001730F4
mov dword ptr [ebp-4Ch],eax -- different for different rows
push dword ptr ds:[4403528h] -- a32.ctor( ... )
mov edx,dword ptr ds:[4403524h]
mov ecx,dword ptr [ebp-4Ch]
call 00790068
push dword ptr [ebp-4Ch] -- aa[32] = a32
mov ecx,dword ptr [ebp-3Ch]
mov edx,20h
call 71CCCD30
These changes doesn't help:
// 1. explicit variable
A a;
a = new A( ... ); // a32 = new A(...); a = a32;
aa[32] = a
// 2. limited visibility
{ aa[32] = new A( ... ); } // for each created object
This change helps but too ugly:
// Works in Release version only
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void f32( A[] aa )
=> aa[32] = new A( qwerty, asdf );
...
f32( aa ); -- inlined without a32
Is any elegant way to remove these stack devourers?
Thanks in advance.
I would like to know what is the value of an out/ref parameter of a invoked method.
When the method is invoked without throws an exception, the value is received in the parameter, but I do not get the value when an exception throws in the invoked method. Invoking directly the method without Reflection, the value is received.
Am I doing something wrong or is this a .net limitation?
using System;
using System.Reflection;
class Program
{
static void Main()
{
string[] arguments = new string[] { bool.FalseString, null };
MethodInfo method = typeof(Program).GetMethod("SampleMethod");
try
{
method.Invoke(null, arguments);
Console.WriteLine(arguments[1]); // arguments[1] = "Hello", Prints Hello
arguments = new string[] { bool.TrueString, null };
method.Invoke(null, arguments);
}
catch (Exception)
{
Console.WriteLine(arguments[1]); // arguments[1] = null, Does not print
}
arguments[1] = null;
try
{
SampleMethod(bool.TrueString, out arguments[1]);
}
catch (Exception)
{
Console.WriteLine(arguments[1]); // arguments[1] = "Hello"
}
}
public static void SampleMethod(string throwsException, out string text)
{
text = "Hello";
if (throwsException == bool.TrueString)
throw new Exception("Test Exception");
}
}
After search a litlle bit I found the solution below. Would be good to use it?
using System;
using System.Reflection;
using System.Reflection.Emit;
public static class MethodInfoExtension
{
public static object InvokeStrictly(this MethodInfo source, object obj, object[] parameters)
{
ParameterInfo[] paramInfos = source.GetParameters();
if ((parameters == null) || (paramInfos.Length != parameters.Length))
{
throw new ArgumentException();
}
Type[] paramTypes = new[] { typeof(object[]) };
DynamicMethod invokerBuilder = new DynamicMethod(string.Empty, typeof(object), paramTypes);
ILGenerator ilGenerator = invokerBuilder.GetILGenerator();
Label exBlockLabel = ilGenerator.BeginExceptionBlock();
for (int i = 0; i < paramInfos.Length; i++)
{
var paramInfo = paramInfos[i];
bool paramIsByRef = paramInfo.ParameterType.IsByRef;
var paramType = paramIsByRef ? paramInfo.ParameterType.GetElementType() : paramInfo.ParameterType;
ilGenerator.DeclareLocal(paramType);
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldc_I4, i);
ilGenerator.Emit(OpCodes.Ldelem_Ref);
Label label1 = ilGenerator.DefineLabel();
ilGenerator.Emit(OpCodes.Brfalse, label1);
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldc_I4, i);
ilGenerator.Emit(OpCodes.Ldelem_Ref);
ilGenerator.Emit(OpCodes.Unbox_Any, paramType);
ilGenerator.Emit(OpCodes.Stloc_S, (byte)i);
ilGenerator.MarkLabel(label1);
if (paramIsByRef)
{
ilGenerator.Emit(OpCodes.Ldloca_S, (byte)i);
}
else
{
ilGenerator.Emit(OpCodes.Ldloc_S, (byte)i);
}
}
LocalBuilder resultLocal = ilGenerator.DeclareLocal(typeof(object), false);
ilGenerator.Emit(OpCodes.Call, source);
if (source.ReturnType == typeof(void))
{
ilGenerator.Emit(OpCodes.Ldnull);
}
ilGenerator.Emit(OpCodes.Stloc_S, resultLocal);
ilGenerator.Emit(OpCodes.Leave, exBlockLabel);
ilGenerator.BeginFinallyBlock();
for (int i = 0; i < paramInfos.Length; i++)
{
var paramInfo = paramInfos[i];
bool paramIsByRef = paramInfo.ParameterType.IsByRef;
var paramType = paramIsByRef ? paramInfo.ParameterType.GetElementType() : paramInfo.ParameterType;
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldc_I4, i);
ilGenerator.Emit(OpCodes.Ldloc_S, (byte)i);
if (paramType.IsValueType)
{
ilGenerator.Emit(OpCodes.Box, paramType);
}
ilGenerator.Emit(OpCodes.Stelem, typeof(object));
}
ilGenerator.EndExceptionBlock();
ilGenerator.Emit(OpCodes.Ldloc_S, resultLocal);
ilGenerator.Emit(OpCodes.Ret);
var invoker = (Func<object[], object>)invokerBuilder.CreateDelegate(typeof(Func<object[], object>));
return invoker(parameters);
}
}
public class Program
{
static void Main()
{
object[] args = new object[1];
try
{
MethodInfo targetMethod = typeof(Program).GetMethod("Method");
targetMethod.InvokeStrictly(null, args);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine();
}
Console.WriteLine(args[0]);
Console.ReadLine();
}
public static void Method(out string arg)
{
arg = "Hello";
throw new Exception("Test Exception");
}
}
In short, you are not doing anything wrong. Its a limitation on the implementation on invoke.
When using ref in a direct call the reference of your local value gets passed into the method. With invoke, for security reasons, a copy is made and copied back into your local reference only if the call did not throw an exception.
For the long answer...
So, taken your example I created this fiddle to view the IL code. That gives us the following:
.method public hidebysig static void SampleMethod(string throwsException, [out] string& text) cil managed
{
//
.maxstack 2
.locals init (bool V_0)
IL_0000: nop
IL_0001: ldarg.1 // Get argument 2
IL_0002: ldstr "Hello" // Get string literal
IL_0007: stind.ref // store in reference address
IL_0008: ldarg.0
IL_0009: ldsfld string [mscorlib]System.Boolean::TrueString
IL_000e: call bool [mscorlib]System.String::op_Equality(string, string)
IL_0013: ldc.i4.0
IL_0014: ceq
IL_0016: stloc.0
IL_0017: ldloc.0
IL_0018: brtrue.s IL_0025
IL_001a: ldstr "Test Exception"
IL_001f: newobj instance void [mscorlib]System.Exception::.ctor(string)
IL_0024: throw
IL_0025: ret
} // end of method Program::SampleMethod
As expected the value of "Hello" gets set in the reference address of the second (output) parameter. Meaning that the thrown exception makes no difference to setting the value or not.
Now for using invoke there is no direct call. I didn't lookup the IL code for this part, but the source is enough to figure out whats going on. First up the Invoke method is called:
public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
{
object[] arguments = InvokeArgumentsCheck(obj, invokeAttr, binder, parameters, culture);
// [Security Check omitted for readability]
return UnsafeInvokeInternal(obj, parameters, arguments);
}
Notice, that it calls the InvokeArgumentsCheck which returns an array of value called arguments. The method is implemented as follows:
internal Object[] CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
{
// copy the arguments in a different array so we detach from any user changes
Object[] copyOfParameters = new Object[parameters.Length];
// [Code omitted for readability]
for (int i = 0; i < parameters.Length; i++)
{
// [Code omitted for readability]
copyOfParameters[i] = argRT.CheckValue(arg, binder, culture, invokeAttr);
}
return copyOfParameters;
}
The method basically creates an copy of the input parameters you specified (with various type checks in place). As you can see from the comment placed in the method, this is done to prevent any changes by the user from influencing the data will the method is being called.
As last we look into the UnsafeInvokeInternal. The method source looks as follows:
private object UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
{
if (arguments == null || arguments.Length == 0)
return RuntimeMethodHandle.InvokeMethod(obj, null, Signature, false);
else
{
Object retValue = RuntimeMethodHandle.InvokeMethod(obj, arguments, Signature, false);
// copy out. This should be made only if ByRef are present.
for (int index = 0; index < arguments.Length; index++)
parameters[index] = arguments[index];
return retValue;
}
}
As we have arguments, we can focus on the 'else' part. The method is invoked by passing the arguments which is, as we determined before, a copy of the provided parameters. After the call is complete, the argument values are pushed back into the source array 'Parameters'.
In the case of an exception that means that the code gets aborted before it can 'push back' the "Hello" to our output parameter. Most likely (but I've been unable to check) it does change the copied-value in the arguments array, we just cannot access it.
I'll let you decide if this was by design, oversight, or they just thought there shouldn't be a use-case for this anyway.
I am trying to print out the contents of an array after invoking some methods which alter it, in Java I use:
System.out.print(Arrays.toString(alg.id));
how do I do this in c#?
You may try this:
foreach(var item in yourArray)
{
Console.WriteLine(item.ToString());
}
Also you may want to try something like this:
yourArray.ToList().ForEach(i => Console.WriteLine(i.ToString()));
EDIT: to get output in one line [based on your comment]:
Console.WriteLine("[{0}]", string.Join(", ", yourArray));
//output style: [8, 1, 8, 8, 4, 8, 6, 8, 8, 8]
EDIT(2019): As it is mentioned in other answers it is better to use Array.ForEach<T> method and there is no need to do the ToList step.
Array.ForEach(yourArray, Console.WriteLine);
There are many ways to do it, the other answers are good, here's an alternative:
Console.WriteLine(string.Join("\n", myArrayOfObjects));
The easiest one e.g. if you have a string array declared like this string[] myStringArray = new string[];
Console.WriteLine("Array : ");
Console.WriteLine("[{0}]", string.Join(", ", myStringArray));
I decided to test the speeds of the different methods posted here:
These are the four methods I used.
static void Print1(string[] toPrint)
{
foreach(string s in toPrint)
{
Console.Write(s);
}
}
static void Print2(string[] toPrint)
{
toPrint.ToList().ForEach(Console.Write);
}
static void Print3(string[] toPrint)
{
Console.WriteLine(string.Join("", toPrint));
}
static void Print4(string[] toPrint)
{
Array.ForEach(toPrint, Console.Write);
}
The results are as follows:
Strings per trial: 10000
Number of Trials: 100
Total Time Taken to complete: 00:01:20.5004836
Print1 Average: 484.37ms
Print2 Average: 246.29ms
Print3 Average: 70.57ms
Print4 Average: 233.81ms
So Print3 is the fastest, because it only has one call to the Console.WriteLine which seems to be the main bottleneck for the speed of printing out an array. Print4 is slightly faster than Print2 and Print1 is the slowest of them all.
I think that Print4 is probably the most versatile of the 4 I tested, even though Print3 is faster.
If I made any errors, feel free to let me know / fix them on your own!
EDIT: I'm adding the generated IL below
g__Print10_0://Print1
IL_0000: ldarg.0
IL_0001: stloc.0
IL_0002: ldc.i4.0
IL_0003: stloc.1
IL_0004: br.s IL_0012
IL_0006: ldloc.0
IL_0007: ldloc.1
IL_0008: ldelem.ref
IL_0009: call System.Console.Write
IL_000E: ldloc.1
IL_000F: ldc.i4.1
IL_0010: add
IL_0011: stloc.1
IL_0012: ldloc.1
IL_0013: ldloc.0
IL_0014: ldlen
IL_0015: conv.i4
IL_0016: blt.s IL_0006
IL_0018: ret
g__Print20_1://Print2
IL_0000: ldarg.0
IL_0001: call System.Linq.Enumerable.ToList<String>
IL_0006: ldnull
IL_0007: ldftn System.Console.Write
IL_000D: newobj System.Action<System.String>..ctor
IL_0012: callvirt System.Collections.Generic.List<System.String>.ForEach
IL_0017: ret
g__Print30_2://Print3
IL_0000: ldstr ""
IL_0005: ldarg.0
IL_0006: call System.String.Join
IL_000B: call System.Console.WriteLine
IL_0010: ret
g__Print40_3://Print4
IL_0000: ldarg.0
IL_0001: ldnull
IL_0002: ldftn System.Console.Write
IL_0008: newobj System.Action<System.String>..ctor
IL_000D: call System.Array.ForEach<String>
IL_0012: ret
Another approach with the Array.ForEach<T> Method (T[], Action<T>) method of the Array class
Array.ForEach(myArray, Console.WriteLine);
That takes only one iteration compared to array.ToList().ForEach(Console.WriteLine) which takes two iterations and creates internally a second array for the List (double iteration runtime and double memory consumtion)
Starting from C# 6.0, when $ - string interpolation was introduced, there is one more way:
var array = new[] { "A", "B", "C" };
Console.WriteLine($"{string.Join(", ", array)}");
//output
A, B, C
Concatenation could be archived using the System.Linq, convert the string[] to char[] and print as a string:
var array = new[] { "A", "B", "C" };
Console.WriteLine($"{new String(array.SelectMany(_ => _).ToArray())}");
//output
ABC
In C# you can loop through the array printing each element. Note that System.Object defines a method ToString(). Any given type that derives from System.Object() can override that.
Returns a string that represents the current object.
http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx
By default the full type name of the object will be printed, though many built-in types override that default to print a more meaningful result. You can override ToString() in your own objects to provide meaningful output.
foreach (var item in myArray)
{
Console.WriteLine(item.ToString()); // Assumes a console application
}
If you had your own class Foo, you could override ToString() like:
public class Foo
{
public override string ToString()
{
return "This is a formatted specific for the class Foo.";
}
}
If you want to get cute, you could write an extension method that wrote an IEnumerable<object> sequence to the console. This will work with enumerables of any type, because IEnumerable<T> is covariant on T:
using System;
using System.Collections.Generic;
namespace Demo
{
internal static class Program
{
private static void Main(string[] args)
{
string[] array = new []{"One", "Two", "Three", "Four"};
array.Print();
Console.WriteLine();
object[] objArray = new object[] {"One", 2, 3.3, TimeSpan.FromDays(4), '5', 6.6f, 7.7m};
objArray.Print();
}
}
public static class MyEnumerableExt
{
public static void Print(this IEnumerable<object> #this)
{
foreach (var obj in #this)
Console.WriteLine(obj);
}
}
}
(I don't think you'd use this other than in test code.)
I upvoted the extension method answer by Matthew Watson, but if you're migrating/visiting coming from Python, you may find such a method useful:
class Utils
{
static void dump<T>(IEnumerable<T> list, string glue="\n")
{
Console.WriteLine(string.Join(glue, list.Select(x => x.ToString())));
}
}
-> this will print any collection using the separator provided. It's quite limited (nested collections?).
For a script (i.e. a C# console application which only contains Program.cs, and most things happen in Program.Main) - this may be just fine.
this is the easiest way that you could print the String by using array!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace arraypracticeforstring
{
class Program
{
static void Main(string[] args)
{
string[] arr = new string[3] { "Snehal", "Janki", "Thakkar" };
foreach (string item in arr)
{
Console.WriteLine(item.ToString());
}
Console.ReadLine();
}
}
}
If it's an array of strings you can use Aggregate
var array = new string[] { "A", "B", "C", "D"};
Console.WriteLine(array.Aggregate((result, next) => $"{result}, {next}")); // A, B, C, D
that way you can reverses the order by changing the order of the parameters like so
Console.WriteLine(array.Aggregate((result, next) => $"{next}, {result}")); // D, C, B, A
You can use for loop
int[] random_numbers = {10, 30, 44, 21, 51, 21, 61, 24, 14}
int array_length = random_numbers.Length;
for (int i = 0; i < array_length; i++){
if(i == array_length - 1){
Console.Write($"{random_numbers[i]}\n");
} else{
Console.Write($"{random_numbers[i]}, ");
}
}
If you do not want to use the Array function.
public class GArray
{
int[] mainArray;
int index;
int i = 0;
public GArray()
{
index = 0;
mainArray = new int[4];
}
public void add(int addValue)
{
if (index == mainArray.Length)
{
int newSize = index * 2;
int[] temp = new int[newSize];
for (int i = 0; i < mainArray.Length; i++)
{
temp[i] = mainArray[i];
}
mainArray = temp;
}
mainArray[index] = addValue;
index++;
}
public void print()
{
for (int i = 0; i < index; i++)
{
Console.WriteLine(mainArray[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
GArray myArray = new GArray();
myArray.add(1);
myArray.add(2);
myArray.add(3);
myArray.add(4);
myArray.add(5);
myArray.add(6);
myArray.print();
Console.ReadKey();
}
}