So I'm trying to write a C# function print_r() that prints out information about a value passed much in the same way that the PHP print_r() function works.
What I'm doing is taking in an object as an input in to the function, and depending on what type it is, I'll output the value, or loop through an array and print out the values inside the array. I have no problem printing out basic values, but when I try to loop through the object if I detect it is an array, I get an error from C# saying "Error 1 foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'".
Now I'm assuming this is just because object doesn't implement IEnumerable<>, but is there any way that I can process this taking in the input as type object?
This is my current code for the function (the IEnumerable<> part is blank in terms of content, but this is the code that is giving me an error.
Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void print_r(object val)
{
if (val.GetType() == typeof(string))
{
Console.Write(val);
return;
}
else if (val.GetType().GetInterface(typeof(IEnumerable).FullName) != null)
{
foreach (object i in val)
{
// Process val as array
}
}
else
{
Console.Write(val);
return;
}
}
static void Main(string[] args)
{
int[] x = { 1, 4, 5, 6, 7, 8 };
print_r(x);
Console.Read();
}
}
}
val is declared as an Object. After checking if it's an IEnumerable (which you can do simply with is, as shown, but this works also with your original code) you have to cast it explicitly
else if (val is IEnumerable)
{
var e = val as IEnumerable;
foreach (var i in e)
{
Console.WriteLine(i.ToString());
}
}
There is LINQPad that has Dump() extension method, but you can use it only in LINQPad
We can write our own extension method to dump any object to html and view it in browser.
You need to reference LINQPad.exe
public static class Extension
{
public static void Dump<T>(this T o)
{
string localUrl = Path.GetTempFileName() + ".html";
using (var writer = LINQPad.Util.CreateXhtmlWriter(true))
{
writer.Write(o);
File.WriteAllText(localUrl, writer.ToString());
}
Process.Start(localUrl);
}
}
You would have to use reflection to do this I think, I want a similiar function and ouputted my objects into tables using reflection.
I dont have the code to hand but found the basis of my solution here!
Related
.NET Framework 4.7.2. Visual Studio 2019. This issue is not specifically addressed in other posts about this error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stuff
{
class Program
{
static void Main(string[] args)
{
string firstPart = "ABC 123 XYZ";
string firstPartMinusLast = string.Join(" ", firstPart.Split(' ').SkipLast(1));
Console.WriteLine(firstPartMinusLast);
}
}
}
I get an Intellisense error on SkipLast:
string[] does not contain a definition for 'SkipLast' and no
accessible extension method 'SkipLast' accepting a first argument of
type 'string[]' could be found (are you missing a using directive or
an assembly reference?)
Per any documentation I can find online, using System.Linq is supposed to cover this method?
If you check the purple box at the top of the Enumerable.SkipLast documentation, you'll see that it is not available in .NET Framework 4.7.2
For what it's worth, the source code is here, so you can just add it yourself. The logic is brilliant:
On the first call to the enumerator, count items are taken from source and added to a queue, then one is dequeued and returned.
On subsequent calls, the next item is added to the queue, and if that wasn't the last item in source, an item is dequeued and returned.
When it's done, the queue contains a cache of the last items in the source (which we wanted to skip), and it was done without knowing ahead of time how many items source contained.
public static class Extensions
{
public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source, int count)
{
var queue = new Queue<T>();
using (var e = source.GetEnumerator())
{
while (e.MoveNext())
{
if (queue.Count == count)
{
do
{
yield return queue.Dequeue();
queue.Enqueue(e.Current);
} while (e.MoveNext());
}
else
{
queue.Enqueue(e.Current);
}
}
}
}
}
i have method where i create random number once called from other class. Making delegate and pointing it to that method invokes that method itself and random number is generated. I can't access that method without creating new random number. I want to get that method returned value with delegate. By writing it "Console.WriteLine(some_kind_delegate);" gives me path "Consoleapp8.class+method".
P.S although when i use delegate when comparing his pointed value with other variable answer is correct.
Screenshot in visual studio environment with my comments: https://www.dropbox.com/s/cx6858x5qen7k1p/dayum.PNG?dl=0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
abstract class variklis
{
delegate int delegatas();
static int litrazas;
static void Main()
{
Console.WriteLine("serijinis bloko numeris: " + blokas.serijinis_bloko_numeris());
Console.WriteLine("variklio tipas: In-line " + blokas.vidus() + " cilindrai");
Console.WriteLine("stumokliu skaicius: " + stumokliai.stumokliuskaicius);
Console.WriteLine("stumokliu kodas: " + stumokliai.stumokliu_kodas());
Console.Write("galimas variklio litrazas siam automobiliui: ");
int.TryParse(Console.ReadLine(), out litrazas);
litrazui();
}
public static void litrazui()
{
string damm;
delegatas zeta;
zeta = blokas.litrazas;
Console.WriteLine(zeta);
if (zeta() <= litrazas)
{
damm = "variklis tinkamas siam automobiliui";
}
else
{
damm = "variklis netinkamas siam automobiliui";
}
Console.WriteLine(damm);
}
}
}
The problem is due to the Console.WriteLine implicitely converting the delegate to a string, which is Consoleapp8.class+method, instead you need to invoke the function be appending parenthesis to the end of it.
Console.WriteLine(zeta());
And to answer the question in your comment. If you need to store the int that is the return from the delegate you can do apply the same principle from above, by appending parenthesis to invoke the function.
int number = zeta();
spent some time again with the scripting interface of my app.
i guess i now have an advanced dlr problem here.
I have a python script
I have an .NET object [o1]
I call a method on the python script from .NET via Iron Python.
The python code creates an object [o2]
The python code calls an method on object [o1] passing [o2] as an argument (Via subclassing DynamicMetaObject)
In the .NET code of the o1-method i want to dynamically call methods on o2
For example i could do that via
((dynamic)o2).FuncInPythonScript
so far so good thats all working.
.NET calls Python (step 3)
Python calls back .NET (step 5)
So i have a basic biderectional control flow between .NET and Python.
We go further:
In the [o1]-method I use LanguageContext.GetMemberNames on [o2]
I wanna call these members somehow via reflection or expressions.
Meaning i dont wanna use the dynamic keyword as in step 7.
Instead somehow call the methods via reflection.
Problem is:
a) I do not know how to get the RuntimeType of the Python-Type, meaning i have no System.Reflection.MethodInfo so i stuck here
b) I try to use LanguageContext.CreateCallBinder and MetaObject.BindInvokeMember so i should have the method 'FuncInPythonScript' bound
But then i'm stuck in how to finally call the bound method.
I see i could use code generation to just generate the code as in step 7, just with the member names from step 8.
But is that really necessary?
I do not see wether approach a) or b) might work or maybe there is somthing i did not think of.
Please do not answer with basic "How do i invoke a python method from .NET" hints.
That is done in steps 1-7 and i have no problem doing this. It's really an advanced problem.
namespace DynamicMetaObjectTest
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Dynamic;
using System.Linq.Expressions;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Providers;
class Program
{
internal sealed class CDotNetObject : IDynamicMetaObjectProvider
{
DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression aExp)
{
return new CInvoker(this, aExp);
}
private sealed class CInvoker : DynamicMetaObject
{
internal CInvoker(CDotNetObject aGws, Expression aExp) : base(aExp, BindingRestrictions.Empty, aGws)
{
this.DotNetObject = aGws;
}
private readonly CDotNetObject DotNetObject;
public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
{
var aMethodInfo = this.GetType().GetMethod("GetSetResultDelegate");
var aExp = Expression.Call(Expression.Constant(this), aMethodInfo);
var aRestrictions = BindingRestrictions.GetTypeRestriction(this.Expression, this.LimitType);
var aMetaObject = new DynamicMetaObject(aExp, aRestrictions);
return aMetaObject;
}
public Action<object> GetSetResultDelegate()
{
return this.DotNetObject.SetResultProvider;
}
}
public void SetResultProvider(object aPythonObject_O2)
{
var aResult = ((dynamic)aPythonObject_O2).GetResult(); // this is for noobs. ;-)
var aMetaObjectProvider = (IDynamicMetaObjectProvider)aPythonObject_O2;
var aMetaObject = aMetaObjectProvider.GetMetaObject(Expression.Constant(aPythonObject_O2));
var aLanguageContext = HostingHelpers.GetLanguageContext(gScriptEngine);
var aMemberNames = aLanguageContext.GetMemberNames(aPythonObject_O2);
var aNonSystemMembers = from aMemberName in aMemberNames where !aMemberName.StartsWith("__") select aMemberName;
foreach (var aMemberName in aNonSystemMembers)
{
Console.WriteLine("Getting function result from Python script: " + aMemberName);
// Now problem:
// P1) How to determine wether its an function or an member variable?
// P2) How to invoke the method respectively get the value of the member variable?
// Your turn ;-)
// some of my failures:
{ // does not work:
//var aVar1Binder = aLanguageContext.CreateGetMemberBinder("GetVar1", false);
//var aVar1Bound = aMetaObject.BindGetMember(aVar1Binder);
//var aCallInfo = new CallInfo(0 , new string[]{});
//var aInvokeBinder = aLanguageContext.CreateCallBinder("GetVar1", false, aCallInfo);
//var aInvokeBound = aMetaObject.BindInvokeMember(aInvokeBinder, new DynamicMetaObject[]{ aVar1Bound});
////var aInvokeExp = Expression.Invoke(Expression.Constant(aInvokeBound), new Expression[] { });
}
{ // does not work
//var aExpandable = (IronPython.Runtime.Binding.IPythonExpandable)aMetaObject;
}
}
}
}
static ScriptEngine gScriptEngine;
static void Main(string[] args)
{
var aScriptRuntime = IronPython.Hosting.Python.CreateRuntime();
// That's the python script from step 1:
var aCode = "class CustomView(object) :" + Environment.NewLine +
"\tdef GetResult(self) :" + Environment.NewLine +
"\t\treturn 42;" + Environment.NewLine + // cuz 42 is the answer to everything ;-)
"DotNetObject.SetResultProvider(CustomView())";
var aEngine = aScriptRuntime.GetEngine("py");
gScriptEngine = aEngine;
var aScope = aEngine.CreateScope();
var aDotNetObject = new CDotNetObject();
aScope.SetVariable("DotNetObject", aDotNetObject);
// That's the invoke to pything from step 3:
aEngine.Execute(aCode, aScope);
}
}
}
I have a litle program in c#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static int count;
static void Main(string[] args)
{
for (int i = 0; i<10; i++)
{
Console.WriteLine(func_count());
Console.ReadKey();
}
}
static int func_count()
{
return count++;
}
}
}
I want to write another simple C# program that will be able JUST to execute the func_count(). The first exe will be allready running, I don't want to execute it inside the second application and reflect it's properties.
In C after getting the right to access the memory region to avoid seg fault I would have to use a pointer to a function - something like:
int (* func_ptr)(); //pointer to function
func_ptr = func_count_address
What's a simple way to do this in C# like above?
Suppose that the first program (the one given) is as is and I can't change the code.
Thank you
Why not simply call the static method: ConsoleApplication1.Program.func_count(). This of course assumes that you reference the assembly where ConsoleApplication is located within your second app and that the method you want to invoke is public (which is currently is NOT).
EDIT: If you may not change the access-modifier of the desired method you may use reflection to invoke it however.
Sth. like this:
MethodInfo m = typeof(ConsoleApplication.Program).GetMethod("func_count", BindingFlags.NonPublic);
object result = m.Invoke(null, yourParams);
Usually you´d need an instance on which that method is executed. Since your method is static it does not need it and therefor the first param to Invoke is NULL.
After asking my first question and reading some links provided I decided to just and port the communication layer from the open source vb.net project I used for a few months to C#. I started off good. Now I do have a problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace Eclipse_5._0
{
class PacketHandler
{
#region Packet Constructors
private delegate void PacketDelegate(int Index, byte[] Data);
private PacketDelegate[] Packet;
public PacketHandler()
{
Packet(Enumerations.ClientPackets.CAddChar) = HandleAddChar;
}
#endregion
#region Packet Methods
public void Handledata(int Index, byte[] Data, int MsgType)
{
ByteBuffer Buff = new ByteBuffer();
Buff.WriteBytes(Data);
if (MsgType < 0)
{
return;
}
if (MsgType >= (int)Enumerations.ClientPackets.CQuit)
{
return;
}
Packet(MsgType).Invoke(Index, Buff.ReadBytes(Buff.Length()));
Buff.Dispose();
}
public void HandleNewAccount(int Index, byte[] Data)
{
//TODO: Add New Player Account File.
}
public void HandleAddChar(int Index, byte[] Data)
{
//TODO: Add New Character to Player Account File.
}
#endregion
}
}
The following line has the error
Packet(Enumerations.ClientPackets.CAddChar) = HandleAddChar;
Any help would be great.
Overall, looks like quite a few problems with this code, but if you can post the error details then it might help us help you along a little sooner. Lets looks at maybe getting you passed this hurdle:
Packet is an array, so it looks like you want access by index like this:
Packet[Enumerations.ClientPackets.CAddChar] = HandleAddChar;
But an array is a reference type, and so needs to be instantiated - meaning even with the above in place you'll get a NullReferenceException. Furthermore, HandleAddChar is a method and requires arguments as per the parameters as part of its definition; and what you're intending to do is not what you would be expressing even with passing the appropriate values. So:
public PacketHandler()
{
Packet = new PacketDelegate[1];
Packet[0] = new PacketDelegate(HandleAddChar);
}
Note that I've removed use of Enumerations.ClientPackets.CAddChar as it is meaningless in this context, the idea is that the array must be instantiated and to an appropriate capacity. Lastly, you don't want to call HandleAddChar here directly, we need a delegate reference, so that's what we create and insert into the array.