我正在为emacs中的C#完成(intellisense)工具.
这个想法是,如果用户键入片段,然后通过特定的击键组合要求完成,则完成工具将使用.NET反射来确定可能的完成.
这样做需要知道正在完成的事物的类型.如果它是一个字符串,那么有一组已知的可能方法和属性; 如果它是一个Int32,它有一个单独的集,依此类推.
使用语义,emacs中提供的代码词法分析器/解析器包,我可以找到变量声明及其类型.鉴于此,可以直接使用反射来获取类型上的方法和属性,然后向用户显示选项列表.(好吧,在 emacs中做的不是那么简单,但是使用在 emacs 中运行PowerShell进程的能力,它变得容易得多.我编写了一个自定义.NET程序集来进行反射,将其加载到powershell中,然后在内部运行elisp emacs可以通过comint向powershell发送命令并读取响应.因此,emacs可以快速获得反射结果.)
当代码var
在声明正在完成的事物中使用时,问题就会到来.这意味着未明确指定类型,并且完成将不起作用.
当使用var
关键字声明变量时,如何可靠地确定实际使用的类型?为了清楚起见,我不需要在运行时确定它.我想在"设计时"确定它.
到目前为止,我有这些想法:
我知道怎么做这一切.但对于编辑器中的每个完成请求,它听起来非常重量级.
我想我每次都不需要新的AppDomain.我可以为单个AppDomain重复使用多个临时程序集,并在多个完成请求中分摊设置和拆除它的成本.这更像是对基本理念的调整.
只需将声明编译到模块中,然后检查IL,以确定编译器推断出的实际类型.这怎么可能?我会用什么来检查IL?
还有更好的想法吗?评论?建议?
编辑 - 进一步考虑这一点,编译和调用是不可接受的,因为调用可能有副作用.因此必须排除第一种选择.
此外,我认为我不能假设.NET 4.0的存在.
更新 - 上面没有提到的正确答案,但Eric Lippert温和地指出,是实现完全保真的类型推理系统.它是在设计时可靠地确定var类型的唯一方法.但是,这也不容易.因为我苦于没有幻想,我要试图建立这样的事情,我把选项2的快捷方式-提取相关声明代码,并编译它,然后检查所产生的IL.
这实际上适用于完成方案的公平子集.
例如,假设在下面的代码片段中,?是用户要求完成的位置.这有效:
var x = "hello there";
x.?
Run Code Online (Sandbox Code Playgroud)
完成意识到x是一个String,并提供适当的选项.它通过生成并编译以下源代码来实现:
namespace N1 {
static class dmriiann5he { // randomly-generated class name
static void M1 () {
var x = "hello there";
}
}
}
Run Code Online (Sandbox Code Playgroud)
......然后用简单的反射检查IL.
这也有效:
var x …
Run Code Online (Sandbox Code Playgroud) 下面的XAML中*(星号)是什么意思?
<ColumnDefinition Width="0.07*"/>
<Grid Height="100" HorizontalAlignment="Left"
Margin="102,134,0,0"
Name="grid1" VerticalAlignment="Top"
Width="354">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40*" />
<ColumnDefinition Width="314*" />
</Grid.ColumnDefinitions>
</Grid>
Run Code Online (Sandbox Code Playgroud) 我想将日期/时间舍入到图表应用程序的最近区间.我想要一个像下面这样的扩展方法签名,以便可以在任何精度级别上实现舍入:
static DateTime Round(this DateTime date, TimeSpan span);
Run Code Online (Sandbox Code Playgroud)
我的想法是,如果我通过十分钟的时间跨度,它将四舍五入到最接近的十分钟间隔.我不能理解我的实施,并希望你们中的一个人之前会写过或使用类似的东西.
我认为楼层,天花板或最近的实施都可以.
有任何想法吗?
编辑:感谢@tvanfosson和@ShuggyCoUk,实现如下:
public static class DateExtensions {
public static DateTime Round(this DateTime date, TimeSpan span) {
long ticks = (date.Ticks + (span.Ticks / 2) + 1)/ span.Ticks;
return new DateTime(ticks * span.Ticks);
}
public static DateTime Floor(this DateTime date, TimeSpan span) {
long ticks = (date.Ticks / span.Ticks);
return new DateTime(ticks * span.Ticks);
}
public static DateTime Ceil(this DateTime date, TimeSpan span) {
long ticks = (date.Ticks + span.Ticks - …
Run Code Online (Sandbox Code Playgroud) 这意味着什么以及如何解决它?
我正在使用TPL任务.
整个错误
通过等待任务或访问其Exception属性,未观察到任务的异常.结果,终结器线程重新抛出了未观察到的异常.
在System.Threading.Tasks.TaskExceptionHolder.Finalize()
mscorlib程序
我有一组带有附加命令和逻辑的控件,它们以相同的方式不断重用.我决定创建一个包含所有常用控件和逻辑的用户控件.
但是我还需要控件才能保存可以命名的内容.我尝试了以下方法:
<UserControl.ContentTemplate>
<DataTemplate>
<Button>a reused button</Button>
<ContentPresenter Content="{TemplateBinding Content}"/>
<Button>a reused button</Button>
</DataTemplate>
</UserControl.ContentTemplate>
Run Code Online (Sandbox Code Playgroud)
但是,似乎无法命名放置在用户控件内的任何内容.例如,如果我以下列方式使用控件:
<lib:UserControl1>
<Button Name="buttonName">content</Button>
</lib:UserControl1>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
无法在元素"Button"上设置Name属性值"buttonName".'Button'属于元素'UserControl1'的范围,在另一个范围内定义时,已经注册了一个名称.
如果我删除buttonName,然后编译,但我需要能够命名内容.我怎样才能做到这一点?
达到10 $ digest()次迭代.中止!
"Watchers在最后5次迭代中解雇了"等意义上有很多支持文本,但是很多文本都是来自各种函数的Javascript代码.有诊断这个问题的经验法则吗?这是一个总是可以减轻的问题,还是应用程序很复杂,应该将此问题视为警告?
我正在查看一些示例C#代码,并注意到一个示例将返回包含在()中.
我一直都这么做:
return myRV;
Run Code Online (Sandbox Code Playgroud)
这样做是否有区别:
return (myRV);
Run Code Online (Sandbox Code Playgroud) 有谁知道如何在WPF(3.5SP1)中数据绑定WebBrowser的.Source属性?我有一个listview,我希望在左边有一个小的WebBrowser,在右边有内容,并在每个WebBrowser的源中为每个绑定到列表项的对象中的URI进行数据绑定.
这是我迄今为止的概念证明,但" <WebBrowser Source="{Binding Path=WebAddress}"
"不编译.
<DataTemplate x:Key="dealerLocatorLayout" DataType="DealerLocatorAddress">
<StackPanel Orientation="Horizontal">
<!--Web Control Here-->
<WebBrowser Source="{Binding Path=WebAddress}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Width="300"
Height="200"
/>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=CompanyName}" FontWeight="Bold" Foreground="Blue" />
<TextBox Text="{Binding Path=DisplayName}" FontWeight="Bold" />
</StackPanel>
<TextBox Text="{Binding Path=Street[0]}" />
<TextBox Text="{Binding Path=Street[1]}" />
<TextBox Text="{Binding Path=PhoneNumber}"/>
<TextBox Text="{Binding Path=FaxNumber}"/>
<TextBox Text="{Binding Path=Email}"/>
<TextBox Text="{Binding Path=WebAddress}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud) 我想阅读我添加到项目根目录的文本文件的第一行.意思是,我的解决方案资源管理器在我的项目中的.cs文件旁边显示.txt文件.
所以,我试着这样做:
TextReader tr = new StreamReader(@"myfile.txt");
string myText = tr.ReadLine();
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它指的是Bin文件夹,我的文件不在那里......我怎样才能使这个工作?:/
谢谢
我需要在代码中设置绑定.
我似乎无法做到这一点.
这是我尝试过的:
XAML:
<TextBox Name="txtText"></TextBox>
Run Code Online (Sandbox Code Playgroud)
代码背后:
Binding myBinding = new Binding("SomeString");
myBinding.Source = ViewModel.SomeString;
myBinding.Mode = BindingMode.TwoWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(txtText, TextBox.TextProperty, myBinding);
Run Code Online (Sandbox Code Playgroud)
视图模型:
public string SomeString
{
get
{
return someString;
}
set
{
someString= value;
OnPropertyChanged("SomeString");
}
}
Run Code Online (Sandbox Code Playgroud)
我设置它时,属性不会更新.
我究竟做错了什么?