- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设您有一个名为 primitiveType
的 Type
且 primitiveType.IsPrimitive == true
,您如何最简洁地(不使用第三个-party library)创建一个非零值的实例(例如值= 1)?
也就是说,该函数可能如下所示:
public static object CreateNonZero(Type primitiveType)
{
if (!primitiveType.IsPrimitive)
{ throw new ArgumentException("type must be primitive"); }
// TODO
}
也就是说,它应该适用于所有原始值类型,例如bool, byte, sbyte, short, ushort, int, uint, long, ulong, float, double, IntPtr, UIntPtr, char 等
最佳答案
Convert.ChangeType(1, primitiveType)
请注意,如果您想让返回类型与您的实际类型匹配而不是 object
,那么做一个通用版本相对容易:
public static T CreateNonZero<T>()
{
return (T)Convert.ChangeType(1, typeof(T));
}
如果你想处理 IntPtr
和 UIntPtr
,我不知道有什么比显式检查类型更优雅的方法了
public static object CreateNonZero(Type type)
{
if(type == typeof(IntPtr))
return new IntPtr(1);
if(type == typeof(UIntPtr))
return new UIntPtr(1);
return Convert.ChangeType(1, type);
}
关于c# - 在 C# 中给定 Type 创建任何数字/原始值类型的非零实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23808396/
所以我得到了这个形状为 (31641600,2) 的 numpy 数组,其中有一些零值,如果不是很多的话。 我们称这个数组为 X。 正在做: print len(X) >>> 31641600 然后做
TL;DR:我有一个 IBOutlet ( UILabel ) 在 Storyboard 中正确连接。访问(展开)它在 viewDidLoad() 中工作正常...但几秒钟后它的值为零。一个观察点说,
我想用numpy数组进行非零cumsum。只需跳过数组中的零并应用 cumsum 即可。假设我有一个 np.数组 a = np.array([1,2,1,2,5,0,9,6,0,2,3,0]) 我的结
该代码运行,甚至产生预期的输出。但是,在构建日志中,我总是得到进程终止,状态为 2(0 分钟,5 秒) 或该日志的某些变体。 代码- #include #include void main() { p
我读了一些关于 TCP window scaling 的东西和 BDP (不太清楚),我无法弄清楚到底是什么原因导致发送方的 TCP 实现设置非零 WS,用户模式客户端程序是否会以某种方式影响它?我认
我有一个字典 { 'a': 'a', 'b': 0, 'c': {} } 我需要省略所有具有空值的键(0 是不被认为是空的)。所以,它的输出应该是 { 'a': 'a', 'b': 0 }。 对于 {
我想在 python 中的 pandas 数据帧上应用 cumsum,但没有零。只是我想保留零并在数据帧上执行 cumsum 。假设我有这样的数据框: import pandas as pd df =
我使用 getimagesize 函数获取图像的宽度和高度,如下所示: list($width,$height) = getimagesize($source_pic); 如何使用 IF 条件来检查
在 PHP 中(使用内置函数)我想用小数转换/格式化数字,以便只显示非零小数。但是,我的另一个要求是,如果它是一个没有十进制值的数字,我仍然希望显示为零。例子: 9.000 -> 9.0 9.100
我可以使用 df['TOTAL'] = df.sum(axis=1) 向此 DF 添加一个 TOTAL 列,它会像这样添加行元素: col1 col2 TOTAL 0 1.0 5.0
我正在玩 python 列表,我想在不使用列表时从内存中删除它。(我有大数据列表,可能有数千或数百万个元素..数据类型-> 浮点型) 我试过这段代码,看看删除在 python 中是如何工作的(示例)
我正在研究 Linux 设备驱动程序代码。我无法透露这段代码的具体用途。我会尽力解释我的情况。当我们收到 USB 中断,表明有来自 USB 的数据时,下面的代码将在中断上下文中执行。数据将以 URB
能力 manpage比较长,有些东西我没有完全理解。 例如,决定我们是否有权访问 CAP_NET_RAW 的函数看起来如何? 输入: a = 有效 uid 为 0 b = 有一些真实的/保存的/任何为
我正在创建一个测试用例,用于测试 TVIroom 类的委托(delegate)函数 didDisconnectwithError 是否可以被调用。为此,我需要向委托(delegate)函数传递一个空白
我想知道 numpy.nonzero/numpy.flatnonzero 返回的索引顺序。 我在文档中找不到任何关于它的内容。它只是说: A[nonzero(flag)] == A[flag] 虽然在
假设我有这样的数据框 id p1 p2 p3 p4 1 0 9 0 4 2 0 0 0 4 3 1 3 10 7 4 1 5 3
这个问题在这里已经有了答案: Division of integers in Java [duplicate] (7 个答案) 关闭 9 年前。 我有一个音量控制 slider 来控制 Androi
我们正在运行一个名为 Axe 的工具检查 HTML 页面的有效性和 508 合规性/可访问性。 此错误作为违规出现: Elements should not have tabindex greater
所以我有两个这样的模型 class ModelParent include Mongoid::Document field :name, :type => String has_one :
我是一名优秀的程序员,十分优秀!