我正在实现一个自定义集合实现,可以是readonly或非readonly; 也就是说,所有改变集合的方法都会调用一个与道德相当的函数:
private void ThrowIfReadOnly() {
if (this.isReadOnly)
throw new SomeException("Cannot modify a readonly collection.");
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我不确定应该使用哪种NotSupportedException或者InvalidOperationException我应该使用.
.net c# exception invalidoperationexception notsupportedexception
好的,所以这里是我的问题的一些上下文,写成伪C#代码(随意指出任何错误):(你可以直接跳转到堆栈跟踪并稍后阅读上下文.)
public class SomeForm {
private _model = new ViewModelClass
public void new() {
// Normal Winforms init omitted
ViewModelClassBindingSource.DataSource = _model;
SomeControl1.SetModel(_model);
}
}
public class SomeControl {
private _model = new ViewModelClass
internal void SetModel(ViewModelClass model) {
_model = model;
ViewModelClassBindingSource.DataSource = model;
ViewModelClassBindingSource.ResetBindings(true);
}
}
public class ComplexObject : IPropertyChanging, IPropertyChanged {
public property bool BoolProp {get; set;}
}
public class ViewModelClass : IPropertyChanged {
property IList<ComplexObject> ComplexObjects {get;}
property ComplexObject SelectedComplexObject {get; set;}
property Object SomethingNotNecessarilyRelated …Run Code Online (Sandbox Code Playgroud) 我有这种情况:
private Task LongRunningTask = /* Something */;
private void DoSomethingMore(Task previousTask) { }
public Task IndependentlyCancelableSuccessorTask(CancellationToken cancellationToken)
{
return LongRunningTask.ContinueWith(DoSomethingMore, cancellationToken);
}
Run Code Online (Sandbox Code Playgroud)
特别是,我在这里感兴趣的行为在MSDN的关于Continuation Tasks的页面中详细说明如下:
Canceled在这些场景中,延续进入状态:
- [...]
- 当继续
System.Threading.CancellationToken作为参数传递IsCancellationRequested时,令牌的属性true在继续运行之前.在这种情况下,延续不会开始,它会转换到Canceled状态.
上面的代码有效.但是,我正在尽可能多地使用await关键字转换我的延续.
是否存在等效使用await,允许在等待任务完成之前取消继续?
我使用TraceSource类来登录我的.NET项目.
然而,我从未清楚过的一点是,id该TraceEvent方法中参数的意图是什么.目前,我总是把它设置为0.
但它的预期或典型有用用途是什么?
我可以想到几个可能性:
TraceEventType.(Start|Stop|Suspend|Resume|Transfer)枚举值结合使用;这是我之前的问题的后续内容,但您不需要阅读它来理解那个问题.
我正在设计.NET中的接口,它将从COM应用程序(主要是VB6,但Visual C++ 6也是一种可能性)中使用,我想使用Collection类型作为接口中方法的参数和返回类型.
问题:
当VB6内置集合类型(数组,集合,字典)通过互操作时会发生什么?我目前的猜测是:
System.ArrayMicrosoft.VisualBasic.CollectionSystem.Collections.Hashtable那是对的吗?
IEnumerable,ICollection,IList,IDictionary?我是否能够在VB6中执行For Each迭代这些接口?我应该使用接口的通用或非通用变体吗?我想在利用DLR绑定机制的同时访问对象上的属性.
dynamicC#中的关键字),因为我在编译时不知道属性名称;IDictionary<string, object>我所知,转换为只解决了选择实现该接口的动态类的情况(例如ExpandoObject).这是演示代码:
static void Main(string[] args)
{
dynamic obj = new System.Dynamic.ExpandoObject();
obj.Prop = "Value";
// C# dynamic binding.
Console.Out.WriteLine(obj.Prop);
// IDictionary<string, object>
Console.Out.WriteLine((obj as IDictionary<string, object>)["Prop"]);
// Attempt to use reflection.
PropertyInfo prop = obj.GetType().GetProperty("Prop");
Console.Out.WriteLine(prop.GetValue(obj, new object[] { }));
Console.In.ReadLine();
}
Run Code Online (Sandbox Code Playgroud) 我目前正在使用Microsoft的图表控件 - 它的MSDN文档就在这里
我正在检查SeriesChartType 文档,除了一个问题外,这个文档几乎是不需要的:
Line和FastLine类型有什么区别?预先感谢您的任何帮助.
当类型名称足够重要时,属性名称默认为类型名称是相对常见的做法;
public class User { }
public class UserSession
{
/// <summary>
/// Creates a <see cref="UserSession" /> instance
/// with the given <see cref="User" />
/// </summary>
public UserSession(User user)
{
User = user;
}
public User User { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
我的问题是<see cref="User" />XML 文档中的元素引用了该UserSession.User属性。我应该写什么才能引用该User类型?
我在MSBuild文件中有一个项目列表:
<ItemGroup>
<SubProject Include="**\*.csproj" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
现在,我想在每个项目TargetPath中为每个项目设置元数据属性.
我已经知道如何为每个项目提取目标路径,并将其放在单独的项目列表中:
<Target Name="ExtractTargetPaths">
<MSBuild Projects="%(SubProject.Identity)" Targets="GetTargetPath">
<Output TaskParameter="TargetOutputs" ItemName="SubProjectTargetPath" />
</MSBuild>
</Target>
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够访问"SubProjectTargetPath"作为SubProject项目的元数据,而不是具有单独的项目列表.
也就是说,而不是写这样的:
<SomeTask Parameter="%(SubProjectTargetPath.Identity)" />
Run Code Online (Sandbox Code Playgroud)
我可以这样写:
<SomeTask Parameter="%(SubProject.TargetPath)" />
Run Code Online (Sandbox Code Playgroud) .net ×9
c# ×4
winforms ×2
.net-3.5 ×1
async-await ×1
charts ×1
com-interop ×1
exception ×1
mdi ×1
msbuild ×1
msbuild-task ×1
vb6 ×1