我有一个Windows应用程序访问类库项目中的文件.
public static class Global
{
private static ResourceManager ResourceManager { get; set; }
static Global ()
{
ResourceManager = new ResourceManager("MyClassLibraryProject.Resource", Assembly.GetExecutingAssembly());
}
private static string GetValue (string name)
{
return (ResourceManager.GetString(name, Options.CultureInfo));
}
public static string ProductName
{
get
{
return (GetValue(MethodBase.GetCurrentMethod().Name.Replace("get_", "")));
}
}
}
`
Run Code Online (Sandbox Code Playgroud)
我已ProductName在此示例中手动创建了该属性.是否有更简单的方法来访问资源文件中每行的强类型名称?
有没有办法通过在该单元格中插入公式来获取任何给定单元格的行和列?
因此,单元格A1会说(1,1),C2会读取(3,2)等.这在某种程度上会是一种自我引用.
我正在处理BigInteger类,其数字大约为2,上升到10,000,000.
BigInteger Log函数现在是我算法中最昂贵的函数,我正在拼命寻找替代方案.
因为我只需要日志的组成部分,所以我遇到了这个答案,这在速度方面看起来很棒,但由于某些原因我没有得到准确的值.我不关心小数部分,但我确实需要得到一个准确的积分部分,无论该值是浮动还是上限,只要我知道哪个.
这是我实现的功能:
public static double LogBase2 (System.Numerics.BigInteger number)
{
return (LogBase2(number.ToByteArray()));
}
public static double LogBase2 (byte [] bytes)
{
// Corrected based on [ronalchn's] answer.
return (System.Math.Log(bytes [bytes.Length - 1], 2) + ((bytes.Length - 1) * 8));
}
Run Code Online (Sandbox Code Playgroud)
除角落情况外,这些值现在非常准确.值7到7.99999,15到15.9999,23到23.9999 31到31.9999等返回-Infinity.数字似乎围绕字节边界.知道这里发生了什么吗?
例:
LogBase2( 1081210289) = 30.009999999993600 != 30.000000000000000
LogBase2( 1088730701) = 30.019999999613300 != 30.000000000000000
LogBase2( 2132649894) = 30.989999999389400 != 30.988684686772200
LogBase2( 2147483648) = 31.000000000000000 != -Infinity
LogBase2( 2162420578) = 31.009999999993600 != -Infinity
LogBase2( 4235837212) = …Run Code Online (Sandbox Code Playgroud) 我有一个WinForms应用程序,它使用COM Interop连接到Microsoft Office应用程序.我已经阅读了大量关于如何正确处理COM对象的资料,这里是我的应用程序使用Microsoft自己的文章(这里)中的技术的典型代码:
Excel.Application excel = new Excel.Application();
Excel.Workbook book = excel.Workbooks.Add();
Excel.Range range = null;
foreach (Excel.Worksheet sheet in book.Sheets)
{
range = sheet.Range["A2:Z2"];
// Process [range] here.
range.MergeCells();
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
range = null;
}
// Release explicitly declared objects in hierarchical order.
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
book = null;
excel = null;
// As taken from:
// http://msdn.microsoft.com/en-us/library/aa679807(v=office.11).aspx.
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
Run Code Online (Sandbox Code Playgroud)
已剥离所有异常处理,以使代码更清晰.
循环中的[sheet]对象会发生什么[foreach]?据推测,它不会被清理,也不能在枚举时篡改它.一种替代方法是使用索引循环,但这会产生丑陋的代码,Office对象库中的某些构造甚至不支持索引.
此外,[foreach]循环引用集合[book.Sheets].是否会导致孤儿RCW计数?
这里有两个问题:
[Sheets],[book.Sheets] …这是我第一次编写将用于广泛几何计算的小型不可变结构.我很想使用public readonly字段而不是private field/public getter组合.
public struct Vector4
{
public readonly float Angle, Velocity;
// As opposed to:
private float _Angle, _Velocity;
public float Angle { get { return (this._Angle); } }
public float Velocity { get { return (this._Velocity); } }
public Vector4 (float angle, float velocity)
{
// Once set in the constructor, instance values will never change.
this.Angle = angle;
this.Velocity = velocity;
}
}
Run Code Online (Sandbox Code Playgroud)
它看起来更清洁,并消除了额外的层(吸气剂).如果不使用公共字段是不好的做法,那么以这种方式使用公共只读字段是否有任何负面影响?
请注意,我们只讨论价值类型.例如,数组会通过调用代码来覆盖要覆盖的元素.
更新:
感谢所有的投入.对于没有使用public readonly数据绑定等的情况,使用字段似乎没有任何缺点.在我的基准测试中,执行时间下降了70%,这是一个大问题.针对.NET 4,我原本期望编译器内联getter-only属性.基准测试当然是在发布配置中测试的,没有附加调试器.
在JavaScript中,直接向prototype任何类型添加函数和成员.我试图在TypeScript中实现相同的目的如下:
interface Date
{
Minimum: Date;
}
Date.prototype.Minimum = function () { return (new Date()); }
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误:
Type '() => Date' is not assignable to type 'Date'.
Property 'toDateString' is missing in type '() => Date'.
Run Code Online (Sandbox Code Playgroud)
考虑到TS是强类型的,我们怎么能实现这个目标呢?
由于我在TS中编写自定义实用程序库,所以我宁愿不使用JS.
我经常看到代理调用的代码示例如下所示:
`
public delegate void DelegateThreadActivity<T, U> (T sender, U e);
public event DelegateThreadActivity<Thread, System.EventArgs> OnStart = null;
public event DelegateThreadActivity<Thread, System.EventArgs> OnStop = null;
// Helper method for invocation.
public void RaiseOnStart ()
{
DelegateThreadActivity<Thread, System.EventArgs> instance = null;
try
{
instance = this.OnStart;
// OR
instance = (DelegateThreadActivity) (object) this.OnStart;
if (instance != null)
{
instance(this, System.EventArgs.Empty);
}
}
catch
{
}
}
Run Code Online (Sandbox Code Playgroud)
`
为什么要使用这个[instance]对象?起初我认为这是公司惯例,但看到经验丰富的开发人员也这样做.有什么好处?
如果你想知道一个字节中是否设置了一个特定的位,一个简单的AND掩码可以做到这一点.我想知道是否有更快的方法来实现同样的目标.例如,位移<<或>>返回移位的数字而不是刚刚"掉落"的位.
如果托管代码中没有其他选择,那么编写不安全的程序集会使它更快吗?如果是这样,请解释如何.
上下文:这适用于需要针对生产代码进行高度优化的复杂算法.我认为有必要澄清这一点,以避免可怕的"做你自己的功课"评论和/或投票.
有许多位计数的实现,但在我的情况下,我需要测试一个任意大的数字是否包含至多两个设置位.
我编写了以下函数来完成这项工作并且似乎非常快,但我想知道它是否可以针对C#进一步优化.这个函数在一个循环中被调用几百万次.
public static byte [] BitCountLookupArray = new byte []
{
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, …Run Code Online (Sandbox Code Playgroud) 即使数据被附加到便携式可执行文件,它们仍然有效.据我所知,这是由于可执行代码的偏移+大小性质.
我有一个类似于Winzip的Zip2Exe的可执行打包应用程序.到目前为止,我一直在可压缩有效负载添加到可执行存根的末尾,后跟一个8字节值,表示有效负载的字节大小.
我想要做的是让可执行存根计算自己的大小,并假设剩余的字节作为有效负载而没有任何大小信息.这纯粹是出于好奇,而我真正想知道的是PE可以计算出自己的大小并检测是否有任何添加.
当然,我知道如果通过注入或其他方式更改了它自己的内容,它就无法验证它.
为了澄清这个问题,我想在.NET中使用一个函数来检查Assembly.GetExecutingAssembly().Location和计算附加有效负载的偏移量.如果此偏移量与文件大小相同,则不会有有效负载.
c# ×8
.net ×7
biginteger ×1
bitcount ×1
convention ×1
delegates ×1
excel ×1
field ×1
filesize ×1
interface ×1
interop ×1
invocation ×1
javascript ×1
logarithm ×1
ms-office ×1
optimization ×1
readonly ×1
reference ×1
typescript ×1