当我发现一个奇怪的案例时,我正在玩属性和反射.当我尝试获取自定义属性的构造函数参数时,下面的代码在运行时给了我一个异常.
using System;
using System.Reflection;
class Program
{
[Test(new[] { Test.Foo }, null)]
static void Main(string[] args)
{
var type = typeof(Program);
var method = type.GetMethod("Main", BindingFlags.Static | BindingFlags.NonPublic);
var attribute = method.GetCustomAttributesData()[0].ConstructorArguments;
Console.ReadKey();
}
}
public enum Test
{
Foo,
Bar
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class TestAttribute : Attribute
{
public TestAttribute(Test[] valuesOne, Test[] valuesTwo)
{
}
}
Run Code Online (Sandbox Code Playgroud)
问题似乎是传递给Test属性构造函数的参数.如果其中一个为null,则ConstructorArguments抛出异常.唯一的例外是ArgumentException与name作为异常消息.
这是来自ConstructorArguments调用的堆栈跟踪:
System.RuntimeTypeHandle.GetTypeByNameUsingCARules(String name, RuntimeModule scope)
System.Reflection.CustomAttributeTypedArgument.ResolveType(RuntimeModule scope, String …Run Code Online (Sandbox Code Playgroud) 我正在使用Mono和Gtk#开发应用程序。在我的应用程序内部,我需要使用TreeView控件。我正在寻找一种在单元格内呈现小部件而不是绘制所有内容的方法。我有想要在这些单元格中使用的自定义小部件,并且我不想手动绘制它们。
我查看了文档和论坛,但没有找到一种方法。我认为这是不可能的,因为CellRendererCombo和CellRendererToggle似乎渲染了一个Widget。
谢谢
我有一个生成一些元素的函数,并在List中返回它们.该函数采用参数来过滤返回的元素.该函数如下所示:
let create x = [1..1000] |> List.filter (fun c -> (c % x) = 0)
Run Code Online (Sandbox Code Playgroud)
现在我想用多个标准调用此函数,并将所有结果附加到单个List中.
起初我在想这样做:
let result = (create 100)
|> List.append (create 300)
|> List.append (create 500)
|> List.append (create 3)
printf "%A" result
Run Code Online (Sandbox Code Playgroud)
但我没有发现这个解决方案优雅而重复的List.append表达方式.
我试过第二个解决方案:
let result = [100; 300; 500; 3] |> List.map (fun c -> create c)
|> List.reduce (fun acc c -> acc |> List.append c)
printf "%A" result
Run Code Online (Sandbox Code Playgroud)
这个解决方案更简洁,但有些东西我不喜欢它.我发现它不太清楚,更难以猜测我们试图实现的目标.
所以,我正在寻找第三个解决方案并试图用计算表达式实现一个新的解决方案:
type AppendBuilder() =
member this.Bind (x, f) = f …Run Code Online (Sandbox Code Playgroud)