我想做的是拥有一个对变量的引用数组。我的意思是相当于一个指向 int 的指针的 C 数组(例如)。
示例:(!!不是真正的代码!!)
int a = 4;
int b = 5;
int c = 6;
List<ref int> tmp = new List<ref int>();
tmp.Add(ref a);
tmp.Add(ref b);
tmp.Add(ref c);
tmp[0] = 16;
tmp[1] = 3;
tmp[2] = 1000;
Console.Writeline(tmp[0] + " " + a); // 16 16
Console.Writeline(tmp[1] + " " + b); // 3 3
Console.Writeline(tmp[2] + " " + c); // 1000 1000
Run Code Online (Sandbox Code Playgroud)
我的案例的具体情况:我有一个与字典中的键相对应的字符串列表。我想我想要的是一个元组列表,其中 Type1 是对 int 或 string 的引用,Type2 是对 Textbox 的引用。
我将迭代这个列表,使用字符串从字典中获取值(并使用该数据进行处理),然后将结果存储到 Type1 中。最终我将从这些 …
在这样的回电中:
this.Model.LoadStudent(student_key, (o, a) =>
{
var student = o as Students;
Run Code Online (Sandbox Code Playgroud)
如果我在第一行设置了一个断点,调试器会向我显示我"o"正在查找的内容,并向我显示它的类型,"Students"但是只要它转到下一行并进行转换,结果就是null.为什么?怎么了?
我有一个Func列表,我想添加元素.如果我在Start上添加它们,如下所示,没问题:
public List<System.Func<bool>> conditions = new List<System.Func<bool>>();
void Start()
{
conditions.Add(Iamdead);
conditions.Add(Iamalive);
}
bool Iamdead()
{
...
return ...;
}
bool Iamalive()
{
...
return ...;
}
Run Code Online (Sandbox Code Playgroud)
但我想在没有Start的情况下定义列表,以便我有一个干净的方法列表,我可以将其视为连续的元素.我尝试过经典格式:
public List<System.Func<bool>> conditions = new List<System.Func<bool>>()
{
bool Iamdead()
{
...
return ...;
}
,
bool Iamalive()
{
...
return ...;
}
};
Run Code Online (Sandbox Code Playgroud)
这给了我解析错误
我试过这样的:
public List<System.Func<bool>> conditions = new List<System.Func<bool>>()
{
Iamdead,Iamalive
};
static bool Iamdead()
{
...
return ...;
}
static bool Iamalive()
{
...
return ...;
}
Run Code Online (Sandbox Code Playgroud)
这只有在方法是静态的情况下才有效,但我不希望它们是静态的.没有静态,它不起作用.我似乎无法理解这里的数据结构.谁能告诉我在列表中定义Func的正确方法?
谢谢
我想知道是否有办法避免foreach以下代码中的循环:
List<string> lines1 = new List<string>();
List<string> lines2 = new List<string>();
lines1.AddRange(File.ReadAllLines("in.txt"));
foreach(string s in lines1)
lines2.Add(Regex.Replace(s,"bim(.*)","bom$1");
Run Code Online (Sandbox Code Playgroud)
请注意,循环还需要在处理期间有两个列表.我的目标是将一个正则表达式应用于原位列表中的每个字符串.
我有这些功能:
public List<int> GetInts(int someParam)
public List<int> GetMoreInts(int someParam, int anotherParam)
Run Code Online (Sandbox Code Playgroud)
我更改了这些函数的签名,以便它们获得一个额外的可选参数:
public List<int> GetInts(int someParam, int offset = 0)
public List<int> GetMoreInts(int someParam, int anotherParam, int offset = 0)
Run Code Online (Sandbox Code Playgroud)
现在,我想调用一个包装函数,它将使用附加的可选参数调用这些函数:
public List<int> Wrapper(Func<List<int>> queryFunc)
{
var offset = 5;
return queryFunc(offset);
}
Run Code Online (Sandbox Code Playgroud)
我将以这种方式调用包装器:
List<int> result = Wrapper(() => GetInts(0));
Run Code Online (Sandbox Code Playgroud)
我怎么能完成它?当我不知道Func指向的函数的签名时,如何向Func添加参数?
如果我想这样做的原因是相关的:
我有许多函数,具有不同的签名,查询不同的数据库表.其中一些查询的结果集太大,所以我想使用(MySQL)Limit函数:
'some mysql query'limit offset,batchsize
然后在包装函数中连接结果.所以我在一些函数中添加了额外的参数(offset,batchsize).我希望包装函数将这些参数添加到Func指向的函数中.
编辑: 我不明白投票的原因.
我有多个具有不同签名的函数来查询我的数据库.
问题是在某些情况下,结果集太大而且我得到超时异常.
因为结果集太大,我想得到一小块结果集,然后将它们全部连接到完整的结果集中.
例如:原始结果集大小为500K,导致超时.
我希望得到大小为1K,500倍的结果集.
这就是为什么我需要从Wrapper中控制偏移量,以便在每次迭代时,我可以发送偏移量,该偏移量在每次迭代后递增,作为原始函数的参数.
我正在尝试创建一个函数,当用户在UITextField中输入文本时,标签显示输入的文本.
我该怎么做?
喜欢:
textField.text = "10"
Label.text = "\(textField.text) smthg" //. (10 smthg)
Run Code Online (Sandbox Code Playgroud)
textField.text = "10.56"
Label.text = "\(textField.text) smthg" //. (10.56 smthg)
Run Code Online (Sandbox Code Playgroud) 捎带一个非常相似的问题 ......
我需要从ViewModel生成一个表达式作为搜索谓词传递.我需要能够根据用户提供的内容包含/排除查询参数.例:IQueryable.Where
public class StoresFilter
{
public int[] Ids { get; set; }
[StringLength(150)]
public string Name { get; set; }
[StringLength(5)]
public string Abbreviation { get; set; }
[Display(Name = "Show all")]
public bool ShowAll { get; set; } = true;
public Expression<Func<Store, bool>> ToExpression()
{
List<Expression<Func<Store, bool>>> expressions = new List<Expression<Func<Store, bool>>>();
if (Ids != null && Ids.Length > 0)
{
expressions.Add(x => Ids.Contains(x.Id));
}
if (Name.HasValue())
{
expressions.Add(x => x.Name.Contains(Name)); …Run Code Online (Sandbox Code Playgroud) 我在另一个问题上得到了一个建议,以替换这段代码:
string ent = c.GetType().GetProperty(prop).GetGetMethod().Invoke(c, null).ToString();
Run Code Online (Sandbox Code Playgroud)
使用可以做同样事情的委托(但在性能方面应该快得多)。
这是我到目前为止的想法:
TestClass test = new TestClass (){DummyProp= "appo"};
string prop = "DummyProp";
MethodInfo method = typeof(TestClass ).GetProperty(prop).GetGetMethod();
Func<TestClass , string> getter= (Func<TestClass , string>)
Delegate.CreateDelegate(typeof(Func<TestClass , string>), test, method);
Console.WriteLine(getter(test));
Run Code Online (Sandbox Code Playgroud)
我想要做的是在运行时获取 实例中属性的值TestClass,该属性可以是其中的多个属性之一,需要哪个属性取决于某些条件
问题是我收到以下异常“无法绑定目标方法,因为其签名或安全透明度与委托类型不兼容”。我错过了什么?
我正在寻找一种更优雅的方式,以避免出现以下情况:
def funct(input1, input2, input3, input4, input5):
input1_new = copy.copy(input1)
input2_new = copy.copy(input2)
input3_new = copy.copy(input3)
input4_new = copy.copy(input4)
input5_new = copy.copy(input5)
Run Code Online (Sandbox Code Playgroud)
由于我目前有很多文件,使用非常广泛的函数,我想知道是否没有更多的 pythonic 方法来复制函数中给出的所有输入。
由于堆栈和堆问题,我正在复制。
谢谢!
这里一定要疯了,但我看不出我在下面的代码中做错了什么导致语法错误
CS0828:无法将“方法组”分配给匿名类型属性
我看不到有关代码的任何匿名内容,该属性有一个名称。我分配给它的方法被命名为等。
// A basic function that takes an int and has void return
private static void DoSomething(int d)
{
// .... do stuff
};
// A class is defined with an Action<int> delegate
private class XX
{
public Action<int> YY { get; set; }
}
// An instance of the class which clearly assigns a known method to the property
private static readonly XX xx = new
{
// error "CS0828: Cannot assign 'method group' to anonymous type …Run Code Online (Sandbox Code Playgroud)