我不确定这是否可行但我想迭代一个类并设置一个字段成员属性而不显式引用字段对象:
public class Employee
{
public Person _person = new Person();
public void DynamicallySetPersonProperty()
{
MemberInfo[] members = this.GetType().GetMembers();
foreach (MemberInfo member in members.Where(a => a.Name == "_person"))
//get the _person field
{
Type type = member.GetType();
PropertyInfo prop = type.GetProperty("Name"); //good, this works, now to set a value for it
//this line does not work - the error is "property set method not found"
prop.SetValue(member, "new name", null);
}
}
}
public class Person
{
public string Name { …
Run Code Online (Sandbox Code Playgroud) 可能重复:
查找传递给C#中函数的变量名称
下面的课程包含现场城市.
我需要动态确定字段名称,因为它是在类声明中输入的,即我需要从对象城市的实例中获取字符串"city".
我试图通过检查其在DoSomething()中的类型来做到这一点,但在检查调试器中的Type的内容时找不到它.
可能吗?
public class Person
{
public string city = "New York";
public Person()
{
}
public void DoSomething()
{
Type t = city.GetType();
string field_name = t.SomeUnkownFunction();
//would return the string "city" if it existed!
}
}
Run Code Online (Sandbox Code Playgroud)
下面他们的答案中的一些人问我为什么要这样做.这就是原因.
在我的真实世界中,城市上方有一个自定义属性.
[MyCustomAttribute("param1", "param2", etc)]
public string city = "New York";
Run Code Online (Sandbox Code Playgroud)
我需要在其他代码中使用此属性.要获取属性,我使用反射.在反射代码中我需要输入字符串"city"
MyCustomAttribute attr;
Type t = typeof(Person);
foreach (FieldInfo field in t.GetFields())
{
if (field.Name == "city")
{
//do stuff when we find the field that has …
Run Code Online (Sandbox Code Playgroud) 我正在Web窗体场景中处理此代码:
public static void RegisterRoutes(RouteCollection routes)
{
Route r = new Route("{*url}", new MyRouteHandler());
routes.Add(r);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.gif/{*pathInfo}");
}
Run Code Online (Sandbox Code Playgroud)
首先,谁能告诉我{*pathInfo}的定义在哪里? http://msdn.microsoft.com/en-us/library/cc668201.aspx#url_patterns并没有真正定义它.请问:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)
比赛
/c/xyz.axd and
/b/c/xyz.axd and
/a/b/c/xyz.axd
Run Code Online (Sandbox Code Playgroud)
而
routes.IgnoreRoute("{resource}.axd");
Run Code Online (Sandbox Code Playgroud)
只有比赛
/xyz.axd
Run Code Online (Sandbox Code Playgroud)
其次,在:
{*url}
Run Code Online (Sandbox Code Playgroud)
这是什么意思?整个表达是什么意思.有没有明确解释的地方?
第三,是否需要添加这些表达式以正确忽略路由?我知道如果IgnoreRoutes在它之前或之后出现,{*url}是某种类型的catchall
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.gif/{*pathInfo}");
Route r = new Route("{*url}", new MyRouteHandler());
routes.Add(r);
Run Code Online (Sandbox Code Playgroud) 我试图从一个对象中调用函数MyMethod,但下面的语法都不起作用.下面一定有一个非常明显的错误,但我看不到它.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/jscript">
function MyObject() {
//how do I get one of these to work??
this.MyMethod; //does not work
this.MyMethod(); //does not work either
MyMethod(); //does not work either
this.MyMethod = function () {
alert('It works');
}
}
var test = new MyObject();
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我已经阅读了Joel的文章"绝对最低限度,每个软件开发人员绝对必须知道关于Unicode和字符集(没有借口!)",但仍然不了解所有细节.一个例子将说明我的问题.请看下面这个文件:
替代文字http://www.yart.com.au/stackoverflow/unicode2.png
我在二进制编辑器中打开文件,仔细检查第一个汉字旁边的三个a中的最后一个:
替代文字http://www.yart.com.au/stackoverflow/unicode1.png
乔尔说:
在UTF-8中,0-127的每个代码点都存储在一个字节中.仅使用2,3存储代码点128及以上,实际上最多6个字节.
编辑也说:
如果是这样,是什么表明解释超过2个字节?这是如何用E6后面的字节表示的?
我的汉字是以2,3,4,5或6字节存储的吗?
我有一个非常简单的操作,我尝试将HtmlGenericControl添加到另一个HtmlGenericControl:
protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl div = new HtmlGenericControl("div");
HtmlGenericControl p = new HtmlGenericControl("p");
div.Controls.Add(p);//exception occurs
}
Run Code Online (Sandbox Code Playgroud)
仅在调试器中抛出异常 - 页面本身呈现为好像什么都没发生.但是肯定会发生一些事情,因为在更大的代码部分中,控件没有按照它们应该进行渲染.
实际的例外是:
{InnerText = '((System.Web.UI.HtmlControls.HtmlContainerControl)(div)).InnerText' threw an exception of type 'System.Web.HttpException'}
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么会发生这种情况,因为您应该能够以这种方式相互添加控件.
请注意,从第一个答案,我再次尝试使用ID,但它仍然无法正常工作:
我有更多的信息.使用占位符控件作为父级时,没有例外:
protected void Page_Init()
{
PlaceHolder ph = new PlaceHolder();
HtmlGenericControl p = new HtmlGenericControl("p");
ph.Controls.Add(p);//no problem
}
Run Code Online (Sandbox Code Playgroud) 我对.NET程序集的物理位置有点困惑.拿好旧的LINQ.在我的web.config
文件中它说:
<add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
Run Code Online (Sandbox Code Playgroud)
这是对LINQ的引用.但是这个DLL到底在哪里?上面没有任何路径,它不在我的bin文件夹中.
我也有我认为的第三方汇编参考:
<add assembly="MapInfo.CoreTypes, Version=4.0.0.483, Culture=neutral, PublicKeyToken=F548BCBA69D4B8DA" />
Run Code Online (Sandbox Code Playgroud)
如果它不在bin
文件夹中,我如何知道它在我的机器上的位置?
此外,如果DLL文件在bin
目录中,我可以假设它不需要被引用web.config
吗?
我需要用户在浏览器上录制他们的声音,然后自动将生成的mp3上传到网络服务器.
我在想用户按下一个大的开始记录/停止记录按钮来执行此操作.
然后,这将在用户硬盘上保存文件.然后它将被有效压缩并自动ftp到网站.
是否可以使用ActiveX或Java进行此操作?或者有可用的图书馆吗?
该应用程序适用于可以根据需要安装软件的用户,因此可以安装exe或其他任何软件.
任何建议都非常感谢.
我试图确定我是否有数据库连接泄漏.所以我需要查看打开连接的数量.我有一些简单的测试代码会产生泄漏:
protected void Page_Load(object sender, EventArgs e)
{
for(int i = 0; i < 100; i++)
{
SqlConnection sql = new SqlConnection(@"Data Source=.\SQLExpress;UID=sa;PWD=fjg^%kls;Initial Catalog=ABC");
sql.Open();
}
}
Run Code Online (Sandbox Code Playgroud)
注意没有.Close,这在快速连续运行3次后确实会崩溃.
为了测量泄漏,我正在运行性能监视器并测量SQLServer:常规统计/用户连接:
替代文字http://www.yart.com.au/stackoverflow/counter.png
但是,当我运行我的代码时,这些似乎是零:
替代文字http://www.yart.com.au/stackoverflow/counter1.jpg
我应该改变什么以实际看到连接?
回答
我已经批准了以下答案.即使它不使用性能工具,它对我的使用也足够好.底线是我想看看打开网页后仍有多少连接打开,这就行了.
我写了第一个Hello World Objective C程序.如何添加断点并检查变量?我可以看到一个断点按钮,但不能插入一个.
签
asp.net ×3
c# ×2
.net ×1
assemblies ×1
browser ×1
cjk ×1
class ×1
connection ×1
controls ×1
database ×1
encoding ×1
field ×1
javascript ×1
properties ×1
record ×1
reflection ×1
routing ×1
set ×1
sql ×1
syntax ×1
unicode ×1
variables ×1
voice ×1
webforms ×1
xcode ×1