小编mao*_*o47的帖子

如果底层实现是结构,那么接口类型的变量是否充当值类型?

我正在看这个问题,除了一种奇怪的方式来枚举某些东西之外,op也遇到了麻烦,因为枚举器是一个结构.我知道返回或传递一个struct会使用一个副本,因为它是一个值类型:

public MyStruct GetThingButActuallyJustCopyOfIt() 
{ 
    return this.myStructField; 
}
Run Code Online (Sandbox Code Playgroud)

要么

public void PretendToDoSomething(MyStruct thingy) 
{ 
    thingy.desc = "this doesn't work as expected"; 
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是如果MyStruct实现IMyInterface(例如IEnumerable),这些类型的方法是否会按预期工作?

public struct MyStruct : IMyInterface { ... }

//will caller be able to modify the property of the returned IMyInterface?
public IMyInterface ActuallyStruct() { return (IMyInterface)this.myStruct; }

//will the interface you pass in get its value changed?
public void SetInterfaceProp(IMyInterface thingy)
{
    thingy.desc = "the implementing type is a struct";
}
Run Code Online (Sandbox Code Playgroud)

c# struct

3
推荐指数
1
解决办法
141
查看次数

列表格式错误中的 SmartFormat.NET 嵌套占位符

我正在尝试测试SmartFormat.NET的功能,在尝试格式化视图模型项列表时遇到问题。根据这个交流,我想要完成的事情应该可以通过嵌套占位符来实现。

这是我正在使用的模板:

var smartTemplate = @"
<div>This is the title</div>
<div>model name: {Name}</div>
<div>model description: {Description}</div>
<div>model rating: {Rating}</div>
{ItemList:
    <div>{NestedName} has number equal to {Number}</div>
}";
Run Code Online (Sandbox Code Playgroud)

还有我的视图模型:

public class SimpleTestVM
{
    public string Name { get; set; }
    public string Description { get; set; }
    public int Rating { get; set; }
    public NestedSimpleVM[] ItemList { get; set; } 
}
public class NestedSimpleVM
{
    public string NestedName { get; set; }
    public int …
Run Code Online (Sandbox Code Playgroud)

.net c# string-formatting smartformat.net

2
推荐指数
1
解决办法
1210
查看次数

标签 统计

c# ×2

.net ×1

smartformat.net ×1

string-formatting ×1

struct ×1