我想通过Reflection设置对象的属性,值为type string
.所以,举个例子,假设我有Ship
一个属性为的类Latitude
,它是一个double
.
这是我想做的事情:
Ship ship = new Ship();
string value = "5.5";
PropertyInfo propertyInfo = ship.GetType().GetProperty("Latitude");
propertyInfo.SetValue(ship, value, null);
Run Code Online (Sandbox Code Playgroud)
这样就抛出了ArgumentException
:
"System.String"类型的对象无法转换为"System.Double"类型.
如何将值转换为正确的类型,基于propertyInfo
?
一位同事告诉我这个:
他有一个DropDownList和一个网页上的按钮.这是背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem item = new ListItem("1");
item.Attributes.Add("title", "A");
ListItem item2 = new ListItem("2");
item2.Attributes.Add("title", "B");
DropDownList1.Items.AddRange(new[] {item, item2});
string s = DropDownList1.Items[0].Attributes["title"];
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DropDownList1.Visible = !DropDownList1.Visible;
}
Run Code Online (Sandbox Code Playgroud)
在页面加载时,项目的工具提示正在显示,但在第一次回发时,属性将丢失.为什么会这样,有没有解决方法?
有关String Basics的MSDN文章显示了这一点:
string str = "hello";
string nullStr = null;
string emptyStr = "";
string tempStr = str + nullStr; // tempStr = "hello"
bool b = (emptyStr == nullStr);// b = false;
string newStr = emptyStr + nullStr; // creates a new empty string
int len = nullStr.Length; // throws NullReferenceException
Run Code Online (Sandbox Code Playgroud)
为什么不与null连接抛出空引用异常?它是为了让程序员的生活更轻松,这样他们在连接之前不必检查null吗?
我需要删除包含只读文件的目录.哪种方法更好:
使用DirectoryInfo.Delete()
,或,
ManagementObject.InvokeMethod("Delete")
?
有了DirectoryInfo.Delete()
,我必须手动关闭每个文件的只读属性,但ManagementObject.InvokeMethod("Delete")
似乎不需要.有没有人比另一个人更优先?
示例代码(test.txt是只读的).
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\David\Desktop\");
dir.CreateSubdirectory("Test");
DirectoryInfo test = new DirectoryInfo(@"C:\Users\David\Desktop\Test\");
File.Copy(@"C:\Users\David\Desktop\test.txt", @"C:\Users\David\Desktop\Test\test.txt");
File.SetAttributes(@"C:\Users\David\Desktop\Test\test.txt", FileAttributes.Archive);
test.Delete(true);
Run Code Online (Sandbox Code Playgroud)
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\David\Desktop\");
dir.CreateSubdirectory("Test");
DirectoryInfo test = new DirectoryInfo(@"C:\Users\David\Desktop\Test\");
File.Copy(@"C:\Users\David\Desktop\test.txt", @"C:\Users\David\Desktop\Test\test.txt");
string folder = @"C:\Users\David\Desktop\Test";
string dirObject = "Win32_Directory.Name='" + folder + "'";
using (ManagementObject managementObject = new ManagementObject(dirObject))
{
managementObject.Get();
ManagementBaseObject outParams = managementObject.InvokeMethod("Delete", null,
null);
// ReturnValue should be 0, else failure
if (Convert.ToInt32(outParams.Properties["ReturnValue"].Value) != 0) …
Run Code Online (Sandbox Code Playgroud) 我想替换一些赋值语句,如:
int someNum = txtSomeNum.Text;
int anotherNum = txtAnotherNum.Text;
Run Code Online (Sandbox Code Playgroud)
同
int someNum = Int32.Parse(txtSomeNum.Text);
int anotherNum = Int32.Parse(txtAnotherNum.Text);
Run Code Online (Sandbox Code Playgroud)
使用正则表达式,使用Visual Studio的查找和替换有一个很好的方法吗?我不确定正则表达式是什么.
我想通过指定每个分区中的元素数量将列表分区为列表列表.
例如,假设我有列表{1,2,... 11},并且想要对其进行分区,使得每个集合具有4个元素,最后一个集合尽可能多地填充元素.生成的分区看起来像{{1..4},{5..8},{9..11}}
写这个的优雅方式是什么?
我正在尝试在辅助监视器上设置Windows窗体,如下所示:
private void button1_Click(object sender, EventArgs e)
{
MatrixView n = new MatrixView();
Screen[] screens = Screen.AllScreens;
setFormLocation(n, screens[1]);
n.Show();
}
private void setFormLocation(Form form, Screen screen)
{
// first method
Rectangle bounds = screen.Bounds;
form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
// second method
//Point location = screen.Bounds.Location;
//Size size = screen.Bounds.Size;
//form.Left = location.X;
//form.Top = location.Y;
//form.Width = size.Width;
//form.Height = size.Height;
}
Run Code Online (Sandbox Code Playgroud)
边界的属性似乎是正确的,但在我尝试的两种方法中,这最大化了主监视器上的表单.有任何想法吗?
我正在使用带有特定端口的Visual Studio开发服务器.有没有办法添加虚拟目录?
编辑:
对不起,我不太清楚.我希望能够将一个或多个虚拟目录添加到任意物理目录中.例如:http:// localhost/c_drive /将映射到C:\,http:// localhost:/ d_drive /将映射到D:\等.
asp.net virtual-directory webdev.webserver visual-studio-2008
我正在学习SNMP,并使用它编写一些应用程序.我有一些关于协议的基本问题:
我希望能够通过按Windows窗体中的按钮来推进Powerpoint演示.以下是我从http://bytes.com/topic/c-sharp/answers/272940-open-powerpoint-presentation-c-window-form中找到的一些代码,它打开了一个Powerpoint演示幻灯片:
Microsoft.Office.Interop.PowerPoint.Application oPPT;
Microsoft.Office.Interop.PowerPoint.Presentations objPresSet;
Microsoft.Office.Interop.PowerPoint.Presentation objPres;
//the location of your powerpoint presentation
string strPres = @"filepath";
//Create an instance of PowerPoint.
oPPT = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
// Show PowerPoint to the user.
oPPT.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
objPresSet = oPPT.Presentations;
//open the presentation
objPres = objPresSet.Open(strPres, MsoTriState.msoFalse,
MsoTriState.msoTrue, MsoTriState.msoTrue);
objPres.SlideShowSettings.Run();
Run Code Online (Sandbox Code Playgroud)
但是,我没有找到可以通过幻灯片前进的任何方法.有任何想法吗?
(我真正想做的是使用WiiRemote推进幻灯片,为学生项目).