我在这里疯了.这可能是因为我错过了一些规则,但如果是这样,那么请告诉我.
我正在尝试创建一个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#的关系还没有发展到那么远!
我有一个List<>我设法写入文件.现在我正在尝试读取相同的文件并将其写回List<>.有没有办法做到这一点?任何人都可以帮我开始吗?会有用吗?
我是java的新手(1年前开始使用).我使用的是Intellij IDEA社区版.今天决定安装它的完整版本,出了点问题.它始终保持"Decompiled"模式,不允许编辑文件或使用它.回到社区版.在"反编译模式"中也是如此.你能帮助我如何进入正常模式工作.任何帮助赞赏
我有很多(超过一千个地方)的遗留T-SQL代码只能在实用程序表中INSERT的varchar(8000)列中.我们的需求已经改变,现在该列需要能够处理更大的值.因此,我需要制作该专栏varchar(max).这只是一个普通的数据列,其中没有执行搜索,没有索引,只有一个过程读取它,它是INSERT忘记应用程序(几乎像一个日志条目).
我计划仅在几个实际生成较大数据的地方进行更改,并在处理此列的单个存储过程中进行更改.
varchar(8000)为varchar(max)? T-SQL字符串函数的工作方式相同,LEN(),RTRIM(),SUBSTRING(),等. varchar(8000)?我正在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) 我目前正在创建一个静音打印模块.我正在使用的当前控件是,它确保已经创建了控件句柄(IsHandleCreated).我做了一切来欺骗这一点,没有运气.
您是否有想法如何创建控件的句柄而不在屏幕上显示任何内容?
我在设置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) 如何将DbCommand参数复制到另一个DbCommand,我想要一个DbCommand与我的上一个参数相同的新参数DbCommand.但现在使用不同的sql字符串.
对于使用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个设备上运行
我正在尝试将一个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# ×8
.net ×1
asp.net ×1
asp.net-mvc ×1
controls ×1
datagridview ×1
dbcommand ×1
dictionary ×1
file ×1
handle ×1
java ×1
object ×1
propertygrid ×1
propertyinfo ×1
sql-server ×1
t-sql ×1
xml ×1