假设我有一个包含类型对象的列表Value.Value有一个Name属性:
private List<Value> values = new List<Value> {
new Value { Id = 0, Name = "Hello" },
new Value { Id = 1, Name = "World" },
new Value { Id = 2, Name = "World" },
new Value { Id = 3, Name = "Hello" },
new Value { Id = 4, Name = "a" },
new Value { Id = 5, Name = "a" },
};
Run Code Online (Sandbox Code Playgroud)
现在我想获得所有"重复"值的列表(name属性与前一个元素的name属性相同的元素).
在这个例子中,我想要一个包含两个元素"world"和"a"(id = 2和5)的列表.
这个事件可以用linq吗?当然,我可以这样做.像这样: …
我们公司的一个Windows Forms应用程序在几个月内出现了一个奇怪的问题.该应用程序对我们的大多数客户非常可靠,但在一些PC(主要是无线局域网连接)上,应用程序有时只是没有响应.(您单击UI和窗口会要求您等待或终止该应用程序).
我无法长时间追踪这个问题,但现在我想出了发生了什么.该应用程序有这行代码
// don't blame me for this. Wasn't my code :D
Control.CheckForIllegalCrossThreadCalls = false
Run Code Online (Sandbox Code Playgroud)
并使用一些后台线程来修改控件.
不,我找到了一种方法来重现应用程序停止响应我的开发机器上的错误并将其跟踪到我实际上使用Invoke()在主线程中运行任务的行.
Me.Invoke(MyDelegate, arg1, arg2)
Run Code Online (Sandbox Code Playgroud)
显然有一个线程锁定.删除后
Control.CheckForIllegalCrossThreadCalls = false
Run Code Online (Sandbox Code Playgroud)
语句和重构整个程序使用Invoke()如果从后台线程修改控件,问题(希望)已经消失.
但是,我想知道是否有办法找到这样的错误而不调试每一行代码(即使我在应用程序停止响应后进入调试器我也无法告诉最后发生了什么,因为IDE没有跳转到Invoke()语句)
换一种说法:
如果我的应用程序挂起,我怎么能弄清楚最后执行了哪行代码?
甚至可能在客户PC上.
我知道VS2010提供了一些向后调试功能,也许这将是一个解决方案,但目前我正在使用VS2008.
我有一个
BindingList<T>
Run Code Online (Sandbox Code Playgroud)
绑定到datagridview.我班上的一个属性需要很长时间来计算,所以我对这个动作进行了操作.在计算之后,我引发OnPropertyChanged()事件以通知网格值已准备好.
至少,这就是理论.但是,由于从一个差异线程调用了OnPropertyChanged方法,我在网格的OnRowPrePaint方法中得到了一些已经存在的异常.
任何人都可以告诉我如何在主线程中推出OnPropertyChanged事件吗?我不能使用Form.Invoke,因为类MyClass不知道它在Winforms应用程序中运行.
public class MyClass : INotifyPropertyChanged
{
public int FastMember {get;set;}
private int? slowMember;
public SlowMember
{
get
{
if (slowMember.HasValue)
return slowMember.Value;
else
{
Thread t = new Thread(getSlowMember);
t.Start();
return -1;
}
}
}
private void getSlowMember()
{
Thread.Sleep(1000);
slowMember = 5;
OnPropertyChanged("SlowMember");
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangingEventHandler eh = PropertyChanging;
if (eh != null)
{
eh(this, e);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个Windows窗体应用程序,我在标签中显示一些客户端数据.我设置了label.AutoEllipsis = true.
如果文本比标签长,它看起来像这样:
Some Text
Some longe... // label.Text is actually "Some longer Text"
// Full text is displayed in a tooltip
Run Code Online (Sandbox Code Playgroud)
这就是我想要的.
但现在我想知道标签是否在运行时使用AutoEllipsis功能.我怎么做到这一点?
解
感谢max.现在我能够创建一个控件,试图将整个文本放在一行中.如果有人有兴趣,这是代码:
Public Class AutosizeLabel
Inherits System.Windows.Forms.Label
Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = value
ResetFontToDefault()
CheckFontsizeToBig()
End Set
End Property
Public Overrides Property Font() As System.Drawing.Font
Get
Return MyBase.Font
End Get
Set(ByVal value As System.Drawing.Font)
MyBase.Font = value
currentFont = value
CheckFontsizeToBig()
End Set …Run Code Online (Sandbox Code Playgroud) 我有下表:
id | parent_id | quantity
-------------------------
1 | null | 5
2 | null | 3
3 | 2 | 10
4 | 2 | 15
5 | 3 | 2
6 | 5 | 4
7 | 1 | 9
Run Code Online (Sandbox Code Playgroud)
现在我需要一个mysql中的存储过程,它以递归方式调用自身并返回计算出的数量.例如,id 6具有5作为父亲,其中3作为父亲,其中2作为父亲.所以我需要计算4 * 2 * 10 * 3(= 240)作为结果.
我对存储过程相当新,我将来不会经常使用它们,因为我更喜欢在程序代码中使用业务逻辑而不是在数据库中.但在这种情况下,我无法避免它.
也许一个mysql大师(就是你)可以在几秒钟内完成一个工作语句.
我最近更改了我的应用程序,使用自定义SplashScreen(它只是一个带有Timer的表单,主窗体并自行关闭)到应用程序框架.
这是我做的:
这完全符合我的要求.SplashScreen首先显示,而不是启动事件触发并且它是否正常工作.SplashScreen关闭,显示实际的主窗体.
到现在为止还挺好.但我们的客户有时会在启动期间遇到这个令人讨厌的异常:
System.InvalidOperationException: Invoke oder BeginInvoke kann für ein Steuerelement erst aufgerufen werden, wenn das Fensterhandle erstellt wurde.
bei System.Windows.Forms.Control.WaitForWaitHandle(WaitHandle waitHandle)
bei System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
bei System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
bei System.Windows.Forms.Control.Invoke(Delegate method)
bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.HideSplashScreen()
bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.MainFormLoadingDone(Object sender, EventArgs e)
bei System.EventHandler.Invoke(Object sender, EventArgs e)
bei System.Windows.Forms.Form.OnLoad(EventArgs e)
bei System.Windows.Forms.Form.OnCreateControl()
bei System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
bei System.Windows.Forms.Control.CreateControl()
bei System.Windows.Forms.Control.WmShowWindow(Message& m)
bei System.Windows.Forms.Control.WndProc(Message& m)
bei System.Windows.Forms.ScrollableControl.WndProc(Message& m)
bei …Run Code Online (Sandbox Code Playgroud) 是否有(首选免费)工具可以分析方法中可能有多少种不同的组合?我目前正在重构一个有很多if/switch语句的方法,我很好奇这个方法有多少可能的不同执行方式.
假设我有一个简单的方法:
public void DoSomething(bool flag1, int value)
{
if (flag1)
{
if (value > 0)
{
Console.WriteLine("Flag1 & value > 0");
return;
}
else
{
Console.WriteLine("Flag1 & value <= 0");
return;
}
}
elseif (value > 0 and value < 10)
{
Console.WriteLine("Flag1 is false and value between 0 & 10");
return;
}
if (value < 0)
{
Console.WriteLine("Flag1 = false & value <= 0");
return;
}
elseif(value = 0)
{
Console.WriteLine("Flag1 = false & value >= 10");
return; …Run Code Online (Sandbox Code Playgroud) 我有一个方法应该从List返回ID.通常我会使用反射来完成这个任务(我不能使用泛型方法,因为类通常是不共享接口或基类的POCOS,我不能修改它们).但是,我想到了新的dynamic关键字,并想尝试这个.
但是我的问题是dataSource [index]返回一个对象.在运行时,确保对象本身是我自己的类并具有id属性.但我想因为该方法返回一个对象,我在访问时会在运行时获得RumtineBinderExceptioncurrent.id
public List<int> GetItemIds()
{
var result = new List<int>();
var dataSource = GetDataSource(); // returns an List<Object>
for (int i = 0; i <= dataSource.Count - 1; i++)
{
dynamic current = dataSource[i];
int id = current.Id; // throws RuntimeBinderException: Object has no definition for id
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法实现我想要或做的事情我必须回到反思来获取id属性?
更新:
current.GetType() returns object
current.GetType().GetProperties() returns a TargetInvocationException
Run Code Online (Sandbox Code Playgroud)
我的Pocos住在我的主项目(VB.net)中,但是这个方法是在类库中,也许这就是原因.然而:
object current = dataSource[i];
PropertyInfo prop = current.GetType().GetProperty("id", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if …Run Code Online (Sandbox Code Playgroud) 我已经部署了ClickOnce Windows窗体应用程序(App A)
另一个应用程序(App B)以文件名作为参数启动App A. 我使用此代码执行此操作
var basePath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
var location = String.Format(@"{0}\{1}\{2}\{3}",
basePath, "MyCompany", "MyProduct", "MyApp.appref-ms");
var fileName = @"c:\temp\somefile.ext";
var uri = new Uri(fileName).ToString();
Process.Start(location, uri);
Run Code Online (Sandbox Code Playgroud)
应用程序A从中获取文件名AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0]并显示内容.
这就像一个魅力.但是,现在我希望App B等待App A退出.但是要求Process.WaitForExit()立即回报.
有没有办法打开ClickOnce应用程序并等待它退出?我可以,如果有必要,改变应用程序的运行方式,但要求是我需要将应用程序作为ClickOnce应用程序运行(我知道在我的用户配置AppData\Local\Apps\2.0\文件文件夹中的某个地方存在exe并且可以直接启动但是如果我这样做的话那ApplicationDeployment.IsNetworkDeployed是false和ApplicationDeployment.CurrentDeploymentnull.因为我放松了ClickOnce更新功能).
我想在运行时通过反射生成类型,引用彼此.
使用静态代码,我会这样做
public class Order
{
public int Id { get; set; }
public Customer Customer { get; set; }
}
public class Customer
{
public int Id { get; set; }
public Order Order { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我可以使用值类型属性成功生成我的Order和OrderDetails Type.
代码看起来像这样
var aName = new System.Reflection.AssemblyName("DynamicAssembly");
var ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName, System.Reflection.Emit.AssemblyBuilderAccess.Run);
var mb = ab.DefineDynamicModule(aName.Name);
var tb = mb.DefineType("Order",
System.Reflection.TypeAttributes.Public, typeof(Object));
var pbId = tb.DefineProperty("Id", PropertyAttributes.None, typeof(int), null);
Run Code Online (Sandbox Code Playgroud)
现在我被困在这条线上:
var pbCustomer = tb.DefineProperty("Customer", PropertyAttributes.None, ???, null);
Run Code Online (Sandbox Code Playgroud)
我需要将属性的类型传递给DefineProperty方法,但此时类型不存在.现在我可以在此时为客户创建一个类型构建器并用于 …