- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我正在尝试编写一个 C# 函数 print_r() 来打印出有关传递的值的信息,其工作方式与 PHP print_r() 函数的工作方式大致相同。
我正在做的是将一个对象作为函数的输入,并根据它的类型输出值,或者循环遍历数组并打印出数组中的值。我打印出基本值没有问题,但是当我尝试循环对象时,如果我检测到它是一个数组,我从 C# 中得到一个错误,说“错误 1 foreach 语句无法对类型为‘object’的变量进行操作,因为‘object’ ' 不包含 'GetEnumerator' 的公共(public)定义。
现在我假设这只是因为对象没有实现 IEnumerable<>,但是有什么方法可以将输入作为类型对象来处理吗?
这是我当前的函数代码(IEnumerable<> 部分在内容方面是空白的,但这是给我一个错误的代码。
谢谢。
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 声明为对象。在检查它是否是 IEnumerable
之后(您可以简单地使用 is
来完成,如图所示,但这也适用于您的原始代码)您必须显式转换它
else if (val is IEnumerable)
{
var e = val as IEnumerable;
foreach (var i in e)
{
Console.WriteLine(i.ToString());
}
}
关于c# - C# 中的 print_r() PHP 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9581014/
我想打印数组变量$foo。所以我已经完成了 {$foo|@print_r} 这个,但是它的打印像 "{$foo|@print_r}" 在首页上。 我正在使用 Oxied eshop 并将 smarty
我正在使用 print_r 作为数组,但它没有在浏览器中以“人类可读”的方式打印它。它只是像我回应的那样打印: Array ( [0] => Array ( [uid1] => 012345 [uid
我希望能够根据 print_r 数组语句的内容生成一个格式良好的表格,其中包含行和列? 有什么想法吗? 最佳答案 这是一种使用 html pre 标记打印漂亮数组的非常简单的方法: '; print_
我正在尝试调试为什么以下查询不返回结果集。当我尝试使用 print_r 调试它以查看此 sql 返回的内容时,我得到了 sql 语句本身而不是结果集。有人可以帮我解决为什么这不返回结果集吗? 编辑:我
这个问题在这里已经有了答案: Make var_dump look pretty (15 个回答) 6 个月前关闭。 我用了print_r函数打印从数据库中的表中获取的数组。而不是像这样格式良好的数组
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: putting print_r results in variable 我正在抛出异常并尝试在异常中包含一个变
我正在尝试调试为什么以下查询不返回结果集。当我尝试使用 print_r 调试它以查看此 sql 返回的内容时,我得到了 sql 语句本身而不是结果集。有人可以帮我解决为什么这不返回结果集吗? 编辑:我
在 Python 中是否有可能做类似的事情: echo 'Content of $var is ', print_r($var, TRUE); 在 PHP 中? 我有一个变量 var,我想将它的内容以
我再次寻求帮助。 我找到了这个停用词脚本 - 我基本上从字符串中删除了所有常用词。 tag’s keyword attribute is not the page rank panacea it o
我在使用 print_r 函数时遇到了一些问题。毫无疑问,我在它的操作中误解了一些东西......基本上,我在一个类中有一个对象数组,如下所示: public $fields = array(); 这
所以我有以下代码: $sql = mysql_query("SELECT result FROM table1"); $result = mysql_fetch_array($sql); print_
当我运行以下代码时: echo $_POST['zipcode']; print_r($lookup->query($_POST['zipcode'])); ?> 结果像这样连接在一行上:10952A
我有一个类,它实际上对一个复杂的数组进行操作,以使操作更简单。原始数组的格式如下所示: array( array( "name" =>"foo", "type"
我试图在我的 php 中获取一个变量转储,所以我正在使用: print_r(get_defined_vars()); 生成它,但无论我在哪里插入代码,它都会导致空白页。有什么想法吗? 最佳答案 其中一
在这种情况下,如何使用 at 符号 (@) - @order 访问属性?这是 print_r 函数的输出: [0] => stdClass Object ( [@order]
这个问题已经有答案了: How create an array from the output of an array printed with print_r? (11 个回答) 已关闭10 年前。
print_r(array('name'=> 'bob', 'age' => 20, 'sex' => 'man')); 然后: Array { [name] => bob, [age
我知道每当我写的时候 $food = array('fruit'=>'apple', 'veggie'=>'tomato', 'bread'=>'wheat'); $text = print_r($f
我在数据库中创建了一个成员表,并输入用户名行作为用户,输入密码行作为密码。然后我编写了一个脚本,必须在数据库中显示密码和用户名。就是这样: "; $username = array($db_field
我正在尝试将 print_r 输出插入数据库? 在数据库中,结果是1? 如何解决这个问题? prepare($SQL); $q->bindValue(":process_id", $p
我是一名优秀的程序员,十分优秀!