小编Irs*_*had的帖子

字典与对象作为价值

我在这里疯了.这可能是因为我错过了一些规则,但如果是这样,那么请告诉我.

我正在尝试创建一个Dictionary带键的字符串和一个匿名对象作为值.或者,实际上我不只是尝试,我正在做.但是当我想改变对象中的特定参数时,它就出错了.

我像这样声明字典:

var areas = new Dictionary<string, object>
{
  {"Key1", new {name = "Name1", today = 0, all = 0}},
  {"Key2", new {name = "Name2", today = 0, all = 0}},
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后我假设我可以这样做:

foreach (var area in areas.Keys.ToArray())
{
    var areaname = areas[area].name;
}
Run Code Online (Sandbox Code Playgroud)

但Visual Studio不同意,并拒绝编译.但是,如果我写这篇文章,一切都按照我的想法运作 - 但这并没有真正帮助我,这只会让我更加沮丧.

var areaname = new 
            {
                 name = "Nametest", today = 0, all = 0
            };
var testname = areaname.name;
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?为什么它不适合我?这样做根本不可能吗?

谢谢你的所有答案,他们肯定清理了一下!我想我可能一直在混淆类型对象对象的想法,即C#中的类,而不是挫折.我将重新思考整个设计业务,并可能做一些完全不同的事情,而不是使用邪恶或肮脏的解决方案.虽然有趣,但我认为我与C#的关系还没有发展到那么远!

c# dictionary object

15
推荐指数
4
解决办法
7万
查看次数

如何读取XML文件并写入List <>?

我有一个List<>我设法写入文件.现在我正在尝试读取相同的文件并将其写回List<>.有没有办法做到这一点?任何人都可以帮我开始吗?会有用吗?

c# xml

13
推荐指数
3
解决办法
7万
查看次数

Intellij IDEA"Decompiled.class文件,字节码转换:52.0(Java 8

我是java的新手(1年前开始使用).我使用的是Intellij IDEA社区版.今天决定安装它的完整版本,出了点问题.它始终保持"Decompiled"模式,不允许编辑文件或使用它.回到社区版.在"反编译模式"中也是如此.你能帮助我如何进入正常模式工作.任何帮助赞赏

在此输入图像描述

java intellij-idea

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

任何隐藏的陷阱将列从varchar(8000)更改为varchar(max)?

我有很多(超过一千个地方)的遗留T-SQL代码只能在实用程序表中INSERTvarchar(8000)列中.我们的需求已经改变,现在该列需要能够处理更大的值.因此,我需要制作该专栏varchar(max).这只是一个普通的数据列,其中没有执行搜索,没有索引,只有一个过程读取它,它是INSERT忘记应用程序(几乎像一个日志条目).

我计划仅在几个实际生成较大数据的地方进行更改,并在处理此列的单个存储过程中进行更改.

  • 是否有任何隐藏的陷阱将列更改varchar(8000)varchar(max)
  • 将所有的T-SQL字符串函数的工作方式相同,LEN(),RTRIM(),SUBSTRING(),等.
  • 任何人都可以想象为什么我必须对认为列仍然存在的代码进行任何更改varchar(8000)

t-sql sql-server sql-server-2005

10
推荐指数
1
解决办法
769
查看次数

获取类型的默认PropertyDescriptors

我正在PropertyGrid通过实现自定义对象类型的显示方式ICustomTypeDescriptor.我允许用户创建自己的自定义属性,这些属性存储在单个键和值字典中.我能够PropertyDescriptors为这些值创建所有值并在属性网格中查看它们.但是,我还想显示所有默认属性,如果PropertyGrid通过反射填充而不是覆盖ICustomTypeDescriptor.GetProperties方法,则会显示这些属性.

现在我知道如何获取对象的类型,然后GetProperties(),但这会返回一个PropertyInfonot 数组ProperyDescriptor.那么如何PropertyInfo将该类型的对象转换为PropertyDescriptor对象以包含到我的集合中PropertyDescriptors

//gets the local intrinsic properties of the object
Type thisType = this.GetType();
PropertyInfo[] thisProps = thisType.GetProperties();

//this line obviously doesn't work because the propertydescriptor 
//collection needs an array of PropertyDescriptors not PropertyInfo
PropertyDescriptorCollection propCOl = 
    new PropertyDescriptorCollection(thisProps);
Run Code Online (Sandbox Code Playgroud)

c# propertygrid propertyinfo propertydescriptor

9
推荐指数
1
解决办法
8229
查看次数

强制创建控件的句柄

我目前正在创建一个静音打印模块.我正在使用的当前控件是,它确保已经创建了控件句柄(IsHandleCreated).我做了一切来欺骗这一点,没有运气.

您是否有想法如何创建控件的句柄而不在屏幕上显示任何内容?

c# controls handle

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

DatagridView选择最后一行

我在设置datagridview中的最后一行时遇到了一些麻烦.我这样选择最后一行:

if (grid.Rows.Count > 0)
{
    try
    {
        grid.Rows[grid.Rows.Count - 1].Selected = true;
        grid.CurrentCell = grid.Rows[grid.Rows.Count - 1].Cells[1]
    }
    catch (IndexOutOfRangeException)
    { }
    catch (ArgumentOutOfRangeException)
    { }
}
Run Code Online (Sandbox Code Playgroud)

当我执行此代码时,我得到一个异常IndexOutOfRangeException occurred:: Index-1没有值.

当我调试Rows集合和相应的Cells集合时,我看到两个集合都已填充.该索引还存在Rows和Cells集合.

我不知道我在这里做错了什么.谁可以帮助我在这里?日Thnx

编辑:

这是完整的例外:

System.IndexOutOfRangeException: Index -1 does not have a value.
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.get_Current()
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
at …
Run Code Online (Sandbox Code Playgroud)

c# datagridview

9
推荐指数
2
解决办法
6万
查看次数

将参数从DbCommand复制到另一个DbCommand

如何将DbCommand参数复制到另一个DbCommand,我想要一个DbCommand与我的上一个参数相同的新参数DbCommand.但现在使用不同的sql字符串.

.net c# enterprise-library dbcommand input-parameters

9
推荐指数
1
解决办法
7754
查看次数

同时读写文件

对于使用File作为某种公司设备预留的全局存储的应用程序,我需要一种方法来读取和写入文件(或锁定文件,从中读取,写入并解锁).一个小代码片段将拍摄我的意思:

FileStream in = new FileStream("storage.bin", FileMode.Open);
//read the file
in.Close();

//!!!!!
//here is the critical section since between reading and writing, there shouldnt
//be a way for another process to access and lock the file, but there is the chance
//because the in stream is closed
//!!!!!
FileStream out = new FileStream("storage.bin", FileMode.Create);
//write data to file
out.Close();
Run Code Online (Sandbox Code Playgroud)

这应该是这样的

LockFile("storage.bin");
//read from it...
//OVERwrite it....
UnlockFile("storage.bin");
Run Code Online (Sandbox Code Playgroud)

该方法应该是绝对安全的,因为程序应该同时在2000个设备上运行

c# file

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

在web api中传递Dictionary <string,string>参数

我正在尝试将一个Dictionary<string,string>对象作为参数传递给我的web api方法,但是如果我检查日志文件,它总是会计数为0:

Web api方法:

[HttpPost]
[ActionName("SendPost")]
public void SendPost([FromBody] Dictionary<string,string> values)
{
    using (var sw = new StreamWriter("F:\\PostTest.txt", true))
    {
        sw.WriteLine("Number of items in the dictionary - " + values.Count);
    }
}
Run Code Online (Sandbox Code Playgroud)

调用web api的逻辑:

public HttpResponseMessage Send(string uri, string value)
{
    HttpResponseMessage responseMessage = null;

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(URI);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var content = new FormUrlEncodedContent
            (
                new Dictionary<string, string> { { "value", value } }
            );

        responseMessage = client.PostAsync(uri, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api

8
推荐指数
1
解决办法
6898
查看次数