向所有人提出问题C#向导.我有一个方法,称之为myFunc,它需要可变长度/类型参数列表.myFunc本身的参数签名是myFunc(params object[] args),我在列表上使用反射(例如,想想这有点像printf).
我想要myFunc(1, 2, 3)区别对待myFunc(new int[] { 1, 2, 3 }).也就是说,在myFunc的主体内,我想枚举我的参数的类型,并希望最终得到{int,int,int}而不是int [].现在我得到后者:实际上,我无法区分这两种情况,它们都以int []的形式出现.
我希望前者显示为obs [].长度= 3,obs [0] = 1等.
而且我曾预计后者会显示为obs [].长度= 1,其中obs [0] = {int [3]}
这可以做到,还是我问不可能?
我在RoR中制作了一个分布式的实时系统,它由2台机器组成.
PC A:从相机拍摄图像并将其发送到第二台PC.所以这台机器每隔一秒就发送一个带有params图像的http请求.PC B - 服务器:将图像保存在数据库中.
我的问题是日志文件变得太大,因为日志甚至是params字符串.如何设置记录器来截断参数?或者只是删除它?
抱歉我的英语不好.....我希望有人可以帮助我.再见戴维德伦蒂尼.
我怎样才能将这两种方法都编译好?
public static IEnumerable<string> DoSomething(params string[] args)
{ // do something }
public static IEnumerable<string> DoSomething(this string[] args)
{ // do something }
Run Code Online (Sandbox Code Playgroud)
我得到这个编译错误:
Type 'Extensions' already defines a member called 'DoSomething' with the same parameter types Extensions.cs
Run Code Online (Sandbox Code Playgroud)
所以我可以这样做:
new string[] { "", "" }.DoSomething();
Extensions.DoSomething("", "");
Run Code Online (Sandbox Code Playgroud)
如果没有params方法,我必须这样做:
Extensions.DoSomething(new string[] { "", "" });
Run Code Online (Sandbox Code Playgroud)
更新:根据OR Mapper的答案
public static IEnumerable<string> DoSomething(string arg, params string[] args)
{
// args null check is not required
string[] argscopy = new string[args.Length …Run Code Online (Sandbox Code Playgroud) 我需要通过javascript将params传递回服务器.目前,我将它们传递给javascript,如下所示:
sendParams("<%= params[:q].to_json %>");
Run Code Online (Sandbox Code Playgroud)
然后像这样发送回来:
function sendParams(q){
$.ajax({
url: '/mymodel/myaction',
type: 'post',
data: {'q':q},
contentType: 'json'
});
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我试着像使用任何其他参数一样使用它们:
MyModel.where(params[:q])
Run Code Online (Sandbox Code Playgroud)
但是,即使firebug在POST标签中显示了这个内容,这些参数仍然是空的:
q=%7B%26quot%3Bc%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Ba%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Btitle%26quot%3B%7D%7D%2C%26quot%3Bp%26quot%3B%3A%26quot%3Bcont%26quot%3B%2C%26quot%3Bv%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bvalue%26quot%3B%3A%26quot%3B2%26quot%3B%7D%7D%7D%7D%2C%26quot%3Bs%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Bvotes_popularity%26quot%3B%2C%26quot%3Bdir%26quot%3B%3A%26quot%3Bdesc%26quot%3B%7D%7D%7D
Run Code Online (Sandbox Code Playgroud)
知道为什么这些信息没有被where子句处理?我该怎么做才能使params Rails再次可读?
更新:
Started POST "/publications/search?scroll=active&page=6" for 127.0.0.1 at 2013-0
2-12 22:55:24 -0600
Processing by PublicationsController#index as */*
Parameters: {"scroll"=>"active", "page"=>"6"}
Run Code Online (Sandbox Code Playgroud)
更新2:
问题显然源于此contentType.当我删除它,然后q作为Rails参数发送.不幸的是,q仍然是JSON,导致错误:
undefined method `with_indifferent_access' for #<String:0x686d0a8>
Run Code Online (Sandbox Code Playgroud)
如何将JSON转换为params哈希?
有没有更优雅的方法来确保构造函数始终被调用至少一个值,而不是我在这里得到的值?我这样做了,因为如果没有提供值,我想要编译器错误.
public class MyClass
{
private readonly List<string> _things = new List<string>();
public string[] Things { get { return _things.ToArray(); } }
public MyClass(string thing, params string[] things)
{
_things.Add(thing);
_things.AddRange(things);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
根据评论,我已将代码更改为此...
public class Hypermedia : Attribute
{
private readonly Rel[] _relations;
public IEnumerable<Rel> Relations { get { return _relations; } }
public Hypermedia(Rel relation, params Rel[] relations)
{
var list = new List<Rel> {relation};
list.AddRange(relations);
_relations = list.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud)
在尝试隐藏我想要做的事情之前编辑代码的道歉.从我的代码编辑器直接粘贴更容易!
解析传递的可变参数数量时有哪些规则params?
假设我有代码:
public void Method(params object[] objects) { }
public void Method(IMyInterface intf, params object[] objects) { }
Run Code Online (Sandbox Code Playgroud)
Method(a, b, c)如果a是IMyInterface,如何解决?我可以确定,C#总是会尝试选择大多数匹配的重载吗?
我有以下代码:
@profile.update_attributes(params[:xxxx_profile])
Run Code Online (Sandbox Code Playgroud)
其中xxxx代表男性或女性.基本上,表单提交传递一组female_profile[foo]或一组male_profile[foo],我想相应地更改它.假设我有一个可以插入的字符串代替xxxx,我该如何动态创建这个符号?
谢谢.
道歉 - 我是使用Ruby on Rails的新手.关于它是如何工作仍然有点困惑.
现在,在我看来,在我的滚动条div下,我有这个代码:
#scroller
-@other_images.each do |img|
.thumbnail_wrapper
.thumbnail_image
=image_tag img.image.url(:thumbnail), :class => 'button' , :onClick => "replaceImg(img.id);"
Run Code Online (Sandbox Code Playgroud)
@other_images是一个变量,它包含我想在页面上显示的所有缩略图图像.单击一个将使用自己的大图像刷新其他地方的div.
相应的js函数是:
:javascript
function replaceImg(id){
var url = '/images/refresh/' + id;
new Ajax.Updater('content_image', url);
}
Run Code Online (Sandbox Code Playgroud)
如果我只是写一个有效的网址,这是有效的.但是将参数"id"传递给js函数是行不通的.我很茫然......我错过了什么?
如何将这个rails变量 - img ---传递给我的js?它只是停留在那个循环中.
任何帮助将非常感激.
我已经看过params参数的次数比我说的多,并且在不考虑它的意义的情况下总是将其删除.现在我已经了解了它的目的.我刚学到的是params参数必须是参数列表中的最后一个.但这是我了解指定默认值的参数.例:
MyMethod(string Name, int blah=0).
Run Code Online (Sandbox Code Playgroud)
所以问题是如果我需要在需要使用时指定上面的默认值params,可以这样做吗?如果是这样,必须在最后宣布?例:
MyMethod(int blah=0, params string[] variableData).
Run Code Online (Sandbox Code Playgroud)
再次感谢您的帮助.詹姆士
我有一个rails 4应用程序,它有一个params块,看起来像:
def store_params
params.require(:store).permit(:name, :description, :user_id, products_attributes: [:id, :type, { productFields: [:type, :content ] } ])
end
Run Code Online (Sandbox Code Playgroud)
但是我收到了错误:
ActiveRecord::AssociationTypeMismatch in StoresController#create
ProductField expected, got Array
Run Code Online (Sandbox Code Playgroud)
我试图插入的参数如下:
Parameters: {"utf8"=>"?", "store"=>{"name"=>"fdsaf", "description"=>"sdfd","products_attributes"=>{"0"=>{"productFields"=>{"type"=>"", "content"=>""}}}}, "type"=>"Magazine", "commit"=>"Create store"}
Run Code Online (Sandbox Code Playgroud)
我的模特是
has_many :products)has_many :productFields和belongs_to :store)belongs_to :product)我的观点如下:
<%= f.fields_for :products do |builder| %>
<%= render 'product_fields', f: builder %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
然后product_fields部分:
<%= f.fields_for :productFields do |builder| %>
<%= builder.text_field :type%>
<%= builder.text_area :content …Run Code Online (Sandbox Code Playgroud)