Z
Zolfaghar
Guest
Hi
I typed in some code, which is below and I am still learning about some of the methods in it. I have tried everything I can to run it and debug it, but have not succeeded. I believe the reason was that the user interface is the command line, and I have tried running it via the command line also, and have posted some of the result below. I wonder if someone can validate if the code runs, and let me know I can troubleshoot any issues this code may have. Here is what I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp13
{
internal delegate Object TwoInt32s(Int32 n1, Int32 n2);
internal delegate Object OneString(String s1);
public static class Program
{
public static void Main(string[] args) {
if (args.Length < 2) {
String usage =
@"Usage:" +
"{0} delType methodName [Arg1] [Arg2] " +
"{0} where delType must be TwoInt32s or OneString" +
"{0} if delType is TwoInt32s, methodName must be Add or Subtract" +
"{0} if delType is OneString, methodName must be NumChars or Reverse" +
"{0}" +
"{0} Examples: " +
"{0} TwoInt32s Add 123 and 321" +
"{0} TwoInt32s Subratct 123 and 321" +
"{0} OneString NumChars \"Hello there\"" +
"{0} OneString Reverse \"Hello there\"";
Console.WriteLine(usage, Environment.NewLine);
return;
}
// Convert the delType argument to a delegate type
Type delType = Type.GetType(args[0]);
if (delType == null)
{
Console.WriteLine("Invalid delType argument: " + args[0]);
return;
}
Delegate d;
try
{
// Convert the Arg1 argument to a method
MethodInfo mi = typeof(Program).GetTypeInfo().GetDeclaredMethod(args[1]);
d = mi.CreateDelegate(delType);
}
catch (ArgumentException)
{
Console.WriteLine("Invalid methodName argument: " + args[1]);
return;
}
Object[] callbackArgs = new Object[args.Length - 2];
if (d.GetType() == typeof(TwoInt32s))
{
try
{
for (Int32 a = 2; a < args.Length; a++)
callbackArgs[a - 2] = Int32.Parse(args[a]);
}
catch (FormatException)
{
Console.WriteLine("Parameters must be integers.");
return;
}
}
if (d.GetType() == typeof(OneString))
{
Array.Copy(args, 2, callbackArgs, 0, callbackArgs.Length);
}
try
{
Object result = d.DynamicInvoke(callbackArgs);
Console.WriteLine("Result = " + result);
}
catch (TargetParameterCountException)
{
Console.WriteLine("Incorrect number of parameters specified.");
}
System.Diagnostics.Debugger.Break();
}
private static Object Add(Int32 n1, Int32 n2)
{
return n1 + n2;
}
private static Object Subtract(Int32 n1, Int32 n2)
{
return n1 - n2;
}
private static Object NumChars(String s1)
{
return s1.Length;
}
private static Object Reverse(String s1)
{
return new String(s1.Reverse().ToArray());
}
}
}
Here are my attempts from the command line:
$ ./ConsoleApp13.exe TwoInt32s Add 123 321
Invalid delType argument: TwoInt32s
$ ./ConsoleApp13.exe OneString "Sam"
Invalid delType argument: OneString
$ ./ConsoleApp13.exe OneString
Usage:
delType methodName [Arg1] [Arg2]
where delType must be TwoInt32s or OneString
if delType is TwoInt32s, methodName must be Add or Subtract
if delType is OneString, methodName must be NumChars or Reverse
Examples:
TwoInt32s Add 123 and 321
TwoInt32s Subratct 123 and 321
OneString NumChars "Hello there"
OneString Reverse "Hello there"
Continue reading...
I typed in some code, which is below and I am still learning about some of the methods in it. I have tried everything I can to run it and debug it, but have not succeeded. I believe the reason was that the user interface is the command line, and I have tried running it via the command line also, and have posted some of the result below. I wonder if someone can validate if the code runs, and let me know I can troubleshoot any issues this code may have. Here is what I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp13
{
internal delegate Object TwoInt32s(Int32 n1, Int32 n2);
internal delegate Object OneString(String s1);
public static class Program
{
public static void Main(string[] args) {
if (args.Length < 2) {
String usage =
@"Usage:" +
"{0} delType methodName [Arg1] [Arg2] " +
"{0} where delType must be TwoInt32s or OneString" +
"{0} if delType is TwoInt32s, methodName must be Add or Subtract" +
"{0} if delType is OneString, methodName must be NumChars or Reverse" +
"{0}" +
"{0} Examples: " +
"{0} TwoInt32s Add 123 and 321" +
"{0} TwoInt32s Subratct 123 and 321" +
"{0} OneString NumChars \"Hello there\"" +
"{0} OneString Reverse \"Hello there\"";
Console.WriteLine(usage, Environment.NewLine);
return;
}
// Convert the delType argument to a delegate type
Type delType = Type.GetType(args[0]);
if (delType == null)
{
Console.WriteLine("Invalid delType argument: " + args[0]);
return;
}
Delegate d;
try
{
// Convert the Arg1 argument to a method
MethodInfo mi = typeof(Program).GetTypeInfo().GetDeclaredMethod(args[1]);
d = mi.CreateDelegate(delType);
}
catch (ArgumentException)
{
Console.WriteLine("Invalid methodName argument: " + args[1]);
return;
}
Object[] callbackArgs = new Object[args.Length - 2];
if (d.GetType() == typeof(TwoInt32s))
{
try
{
for (Int32 a = 2; a < args.Length; a++)
callbackArgs[a - 2] = Int32.Parse(args[a]);
}
catch (FormatException)
{
Console.WriteLine("Parameters must be integers.");
return;
}
}
if (d.GetType() == typeof(OneString))
{
Array.Copy(args, 2, callbackArgs, 0, callbackArgs.Length);
}
try
{
Object result = d.DynamicInvoke(callbackArgs);
Console.WriteLine("Result = " + result);
}
catch (TargetParameterCountException)
{
Console.WriteLine("Incorrect number of parameters specified.");
}
System.Diagnostics.Debugger.Break();
}
private static Object Add(Int32 n1, Int32 n2)
{
return n1 + n2;
}
private static Object Subtract(Int32 n1, Int32 n2)
{
return n1 - n2;
}
private static Object NumChars(String s1)
{
return s1.Length;
}
private static Object Reverse(String s1)
{
return new String(s1.Reverse().ToArray());
}
}
}
Here are my attempts from the command line:
$ ./ConsoleApp13.exe TwoInt32s Add 123 321
Invalid delType argument: TwoInt32s
$ ./ConsoleApp13.exe OneString "Sam"
Invalid delType argument: OneString
$ ./ConsoleApp13.exe OneString
Usage:
delType methodName [Arg1] [Arg2]
where delType must be TwoInt32s or OneString
if delType is TwoInt32s, methodName must be Add or Subtract
if delType is OneString, methodName must be NumChars or Reverse
Examples:
TwoInt32s Add 123 and 321
TwoInt32s Subratct 123 and 321
OneString NumChars "Hello there"
OneString Reverse "Hello there"
Continue reading...