我正在编写一个系统,它要求我获取对象中的属性值,最好使用反射。这个项目是针对 xbox360 的,它在紧凑的框架上运行,因此有一个缓慢的垃圾收集器 - 这意味着我避免分配是绝对重要的!
我发现做到这一点的唯一方法是:
Foo Something; //an object I want to get data from
PropertyInfo p; //get this via reflection for the property I want
object value = p.GetGetmethod().Invoke(Something, null);
//Now I have to cast value into a type that it should be
Run Code Online (Sandbox Code Playgroud)
我不喜欢这个有两个原因:
是否有一些通用方法可以从属性中获取值,并且不会对基元进行装箱?
编辑::为了回应 Jons 的回答,从他的博客中窃取的代码不会导致分配,问题已解决:
String methodName = "IndexOf";
Type[] argType = new Type[] { typeof(char) };
String testWord = "TheQuickBrownFoxJumpedOverTheLazyDog";
MethodInfo method = typeof(string).GetMethod(methodName, argType);
Func<char, int> converted = (Func<char, int>)Delegate.CreateDelegate
(typeof(Func<char, …Run Code Online (Sandbox Code Playgroud) 我试图关闭响应使用Context.Response.End但收到错误"Thread was being aborted".
如何在不触发异常的情况下正确关闭响应?
try {
Context.Response.Clear();
Context.Response.ContentType = "text/html";
//Context.Response.ContentType = "application/json";
JsonObjectCollection collection = new JsonObjectCollection();
collection.Add(new JsonNumericValue("resultcode", 1));
collection.Add(new JsonStringValue("sourceurl", exchangeData.cUrl));
collection.Add(new JsonStringValue("filename", fileName));
collection.Add(new JsonStringValue("filesize", fileSize));
collection.Add(new JsonStringValue("fileurl", Common.GetPDFURL + outputFileName));
JsonUtility.GenerateIndentedJsonText = true;
Context.Response.Write(collection);
try {
Context.Response.End();
} catch (ThreadAbortException exc) {
// This should be first catch block i.e. before generic Exception
// This Catch block is to absorb exception thrown by Response.End
}
} catch (Exception err) { …Run Code Online (Sandbox Code Playgroud) 请帮我看一下VSTO Excel中的C#代码.无论我在哪里选择一个单元格,相应的列都会聚焦,我需要在我关注的任何地方获取Excel工作表上的Column值(列)和Row值(行号).
我怎么能通过代码做同样的事情?如何使用C#在VSTO excel中获取聚焦或当前单元格的列值?
此外,我想学习使用C#的VSTO Excel,因此欢迎一些好的免费/可下载电子书和/或任何网站链接建议.
我想在scala中重写一下Sun的关于java中并发性的教程的例子.原始代码在这里:http://java.sun.com/docs/books/tutorial/essential/concurrency/deadlock.html
此代码不正确.它会在评论指示的位置冻结.有人可以纠正这个吗?提前致谢.
import scala.actors.Actor
class Person(val name: String) extends Actor {
def bow(other: Person) {
other ! Bow(this)
}
private def bowBack(backTo: Person) {
println(this.name + " is bowing back to " + backTo.name)
backTo ! Bowed(this)
}
def act() {
while (true) {
receive {
case Bow(p) =>
println(this.name + " is bowing to " + p.name)
p ! BowBack(this)
println(" wating for bowing back...")
var received = false
while (true && received == false) { …Run Code Online (Sandbox Code Playgroud) 我使用下面的代码来验证 ASP.NET 中的整数和浮点数,但如果我不输入小数,则会出现错误。
<asp:TextBox ID="txtAjaxFloat" runat="server" />
<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" TargetControlID="txtAjaxFloat" FilterType="Custom, numbers" ValidChars="." runat="server" />
Run Code Online (Sandbox Code Playgroud)
我还有来自什么是验证货币、浮点数或整数的 C# 正则表达式?,但如果我在小数点后仅输入一个值,则会出现验证错误。
我越是想到它越多我认为可以编写一个使用这些URL定义的自定义路由:
{var1}/{var2}/{var3}
Const/{var1}/{var2}
Const1/{var1}/Const2/{var2}
{var1}/{var2}/Const
Run Code Online (Sandbox Code Playgroud)
以及任何上层网址中的任何位置最多只有一个贪心参数
{*var1}/{var2}/{var3}
{var1}/{*var2}/{var3}
{var1}/{var2}/{*var3}
Run Code Online (Sandbox Code Playgroud)
有一个重要的约束.具有贪婪段的路径不能包含任何可选部件.所有这些都是强制性的.
这是一个示例性请求
http://localhost/Show/Topic/SubTopic/SubSubTopic/123/This-is-an-example
Run Code Online (Sandbox Code Playgroud)
这是URL路由定义
{action}/{*topicTree}/{id}/{title}
Run Code Online (Sandbox Code Playgroud)
在内部解析请求路由GetRouteData()应该像这样工作:
据我所知,它可以奏效.但我想知道:
我根本没有想过GetVirtuaPath()方法.
我有这个背景,css代码是:
body {
background: #FFF url('images/bg.png') no-repeat bottom center;
margin-bottom: -500px;
width: 100%;
height: 100%;
color:#999;
}
Run Code Online (Sandbox Code Playgroud)
图像高400像素,我希望它与页面底部对齐.
到目前为止,这仅适用于Firefox.在Chrome和IE中,背景位置位于顶部中心,而不是底部中心.
这对于发现错误来说是一个很好的问题.没有?至少对初学者好.
#define SIZE 4
int main(void){
int chars_read = 1;
char buffer[SIZE + 1] = {0};
setvbuf(stdin, (char *)NULL, _IOFBF, sizeof(buffer)-1);
while(chars_read){
chars_read = fread(buffer, sizeof('1'), SIZE, stdin);
printf("%d, %s\n", chars_read, buffer);
}
return 0;
}
使用上面的代码,我试图使用重定向从文件中读取./a.out < data.输入文件的内容:
1line
2line
3line
4line
Run Code Online (Sandbox Code Playgroud)
但是我没有得到预期的输出,而是混合了一些图形字符.有什么问题?
提示:(礼貌Alok)
sizeof('1') == sizeof(int)所以,改用1 :-)
看一下这篇文章,了解使用fread的缓冲IO示例.
有没有办法使用mnesia 进行本地写入和全局读取(无需复制).例如:节点A写入其本地DB,节点B从节点A的DB读取.除了本地存储的架构信息之外,节点B没有自己的任何数据.
根据文档,{local_content, true}似乎我需要使用,但我试图让节点B读取节点A的数据是不成功的.
我的架构和表配置如下所示:
在nodeA @ ip1上:
net_adm:ping('nodeB@ip2').
rd(user, {name, nick}).
mnesia:create_schema([node()|nodes()]).
mnesia:start().
mnesia:create_table(user, [ {local_content, true},
{disc_copies, [node()]},
{attributes,record_info(fields, user) }]).
%% insert data and list rows on nodeA
%% WORKS
Run Code Online (Sandbox Code Playgroud)
在nodeB @ ip2上:
mnesia:start().
%% code to list rows from user table on nodeA
%% throws an ERROR saying table does not exist.
Run Code Online (Sandbox Code Playgroud)
配置是错误的还是可以通过其他方式完成?
asp.net ×2
c# ×2
actor ×1
alignment ×1
asp.net-mvc ×1
c ×1
catch-all ×1
css ×1
eclipse ×1
eclipse-3.4 ×1
erlang ×1
excel ×1
fread ×1
garbage ×1
mnesia ×1
reflection ×1
routing ×1
scala ×1
validation ×1
vsto ×1