小编Dav*_*son的帖子

通过反射使用字符串值设置属性

我想通过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

c# reflection type-conversion propertyinfo setvalue

298
推荐指数
8
解决办法
26万
查看次数

DropDownList中的ListItems属性在回发时会丢失吗?

一位同事告诉我这个:

他有一个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)

在页面加载时,项目的工具提示正在显示,但在第一次回发时,属性将丢失.为什么会这样,有没有解决方法?

asp.net postback behavior listitem drop-down-menu

69
推荐指数
5
解决办法
6万
查看次数

为什么在字符串合法中添加null?

有关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吗?

c# language-design

64
推荐指数
2
解决办法
3万
查看次数

如何在C#中删除包含只读文件的目录?

我需要删除包含只读文件的目录.哪种方法更好:

  • 使用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)

c# directory readonly delete-directory

54
推荐指数
4
解决办法
5万
查看次数

Visual Studio查找和替换正则表达式帮助

我想替换一些赋值语句,如:

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的查找和替换有一个很好的方法吗?我不确定正则表达式是什么.

regex replace visual-studio-2008

38
推荐指数
3
解决办法
2万
查看次数

C# - 分区列表的优雅方式?

我想通过指定每个分区中的元素数量将列表分区为列表列表.

例如,假设我有列表{1,2,... 11},并且想要对其进行分区,使得每个集合具有4个元素,最后一个集合尽可能多地填充元素.生成的分区看起来像{{1..4},{5..8},{9..11}}

写这个的优雅方式是什么?

c# list data-partitioning

35
推荐指数
4
解决办法
3万
查看次数

在辅助监视器上显示Windows窗体?

我正在尝试在辅助监视器上设置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)

边界的属性似乎是正确的,但在我尝试的两种方法中,这最大化了主监视器上的表单.有任何想法吗?

c# multiple-monitors winforms setbounds

34
推荐指数
3
解决办法
8万
查看次数

有没有办法将虚拟目录添加到Visual Studio Development Server?

我正在使用带有特定端口的Visual Studio开发服务器.有没有办法添加虚拟目录?

编辑:

对不起,我不太清楚.我希望能够将一个或多个虚拟目录添加到任意物理目录中.例如:http:// localhost/c_drive /将映射到C:\,http:// localhost:/ d_drive /将映射到D:\等.

asp.net virtual-directory webdev.webserver visual-studio-2008

20
推荐指数
3
解决办法
2万
查看次数

关于SNMP的基本问题

我正在学习SNMP,并使用它编写一些应用程序.我有一些关于协议的基本问题:

  1. 代理商是否将其状态存储在设备本身上?
  2. 如果代理上设置了陷阱,您是否可以对同一个OID进行轮询以获取相同的信息?
  3. 如果不使用mib文件,有没有办法一次查询设备的所有信息?如果没有,并且您正在编写自己的自定义管理器,您是否必须知道它预先报告的结构?
  4. 如果您要设置代理进行报告,通常是否有办法控制发送陷阱的频率?或者它通常会在满足某些条件的情况下发送陷阱?

snmp network-protocols

19
推荐指数
2
解决办法
2819
查看次数

C# - 以编程方式推进Powerpoint幻灯片放映的方式?

我希望能够通过按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推进幻灯片,为学生项目).

.net c# powerpoint interop

14
推荐指数
1
解决办法
1万
查看次数