可能重复:
确定通用参数是否为Nullable类型
我正在尝试确定类型参数是否为Nullable.
public T Get<T>(int index)
{
var none=default(T);
var t = typeof(T);
BaseVariable v = this[index].Var;
if (T is Nullable) //compiler error
{
if (v == ... )
{
return none;
}
}
//....
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?我尝试过,t == typeof(Nullable)但总是导致错误.
我想要发生的是foo.Get<bool?>(1)有时无效.
根据as运营商的文档,as"用于在兼容的参考类型之间执行某些类型的转换".由于Nullable 实际上是一个值类型,我希望as不会使用它.但是,此代码编译并运行:
object o = 7;
int i = o as int? ?? -1;
Console.WriteLine(i); // output: 7
Run Code Online (Sandbox Code Playgroud)
这是正确的行为吗?文件是as错的吗?我错过了什么吗?
在 NPM 模块中,我使用打字稿
"devDependencies": {
"@types/node": "^8.0.0",
"typescript": "^2.8.1"
}
Run Code Online (Sandbox Code Playgroud)
我想使用公共方法返回一个私有的可为空参数。请参考下面的例子。我看到的错误是
Property 'string1' has no initializer and is not definitely assigned in the constructor.
Run Code Online (Sandbox Code Playgroud)
如果我在构造函数中分配了一个 undefined 我得到了错误
[ts]
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'
Run Code Online (Sandbox Code Playgroud)
我应该如何在打字稿中做到这一点,我来自 c# 方面:)
export class HowToDoThis {
private string1?: string;
public constructor() {
//this.string1 = undefined;
}
public add2String1(content: string) {
this.string1 += content;
}
public getString1(): string {
return this.string1;
}
}
Run Code Online (Sandbox Code Playgroud) 如果输入不为空,如何告诉编译器以下扩展方法返回不为空?
public static string? SomeMethod(this string? input)
{
if (string.IsNullOrEmpty(input))
return input;
// Do some work on non-empty input
return input.Replace(" ", "");
}
Run Code Online (Sandbox Code Playgroud) 我必须遍历几个类中的所有属性并检查任何可空属性以查看它们是否具有值.如何将propertyInfo.GetValue()返回的值转换为通用的可空类型,以便我可以检查HasValue属性?
代码剪裁简洁:
foreach (PropertyInfo propInfo in this.GetType().GetProperties())
{
if (<Snip: Check to see that this is a nullable type>)
{
//How do i cast this properly in here to allow me to do:
if(!((Nullable)propInfo.GetValue(this, null)).HasValue)
//More code here
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个SQL数据库表,它有35条现有记录.人在这个表中的字段被称为Name,nvarchar(100),not null
但是,由于最近的更改,我需要使此列可以为空.
当我在SQL Server Management Studio中更改列以允许空值,并转到保存我的更改时,我收到以下错误:
不允许保存更改.您所做的更改需要删除并重新创建以下表
如何允许自动删除和重新创建?
可能重复:可以为
Nullable类型作为通用参数?
我遇到了泛型类型约束非常奇怪的事情.我有一个这样的课:
public SomeClass<T> where T:class
{
}
Run Code Online (Sandbox Code Playgroud)
但是,我发现我不能像我期望的那样使用可空类型:
new SomeClass<int?>();
Run Code Online (Sandbox Code Playgroud)
我得到一个int?必须是引用类型的错误.Nullable真的只是一个带有语法糖的结构,使它看起来像一个引用类型吗?
任何人都可以了解为什么这个单元测试在Visual Studio 2013中失败了?
[TestMethod]
public void Inconceivable()
{
int? x = 0;
Assert.AreEqual(typeof(int?), x.GetType());
}
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序循环通过固定宽度的文本文件,将每一行读取到一个字符串变量并使用.Substring()方法查找给定字段的数据.对于给定的字段,它检查内容是否只是空格,或者其中是否存在实际的"数据",即除了空格之外的任何内容.例如,如果存在数据,并且该数据表示日期,则对该数据运行DateTime.Parse()并将其传递到C#DataTable中的datetime类型的字段; 但是,如果没有数据 - 只是空格,我想简单地将空值传递给该字段.以下是一段代码来说明:
var dataTable = new DataTable();
dataTable.Columns.Add("Application_Date").DataType = Type.GetType("System.DateTime");
while (!sr.EndOfStream)
{
string row = sr.ReadLine();
if (row.Substring(0, 1) == "2" && row.Substring(42, 1) == "T")
{
DataRow dr = dataTable.NewRow();
dr["Application_Date"] = row.Substring(124, 8) != " " ?
DateTime.Parse(row.Substring(124, 4) +
"-" + row.Substring(128, 2) + "-" +
row.Substring(130, 2)) :
null as DateTime?;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我尝试运行它时,它会抛出一个错误,说它想要一个DBNull(Cannot set Column 'Application_Date' to be null. Please use DBNull instead.)
但是当我尝试简单地传递一个DBNull时,它告诉我它无法在DateTime和DBNull(Type of conditional expression cannot be …
我有一个多对一的关系,我想要可以为空:
@ManyToOne(optional = true)
@JoinColumn(name = "customer_id", nullable = true)
private Customer customer;
Run Code Online (Sandbox Code Playgroud)
不幸的是,JPA将我的数据库中的列设置为NOT NULL.有谁能解释一下?有没有办法让它发挥作用?请注意,我使用JBoss 7,JPA 2.0和Hibernate作为持久性提供程序和PostgreSQL 9.1数据库.
编辑:
我找到了问题的原因.显然,这是由于我在引用实体中定义主键的方式Customer:
@Entity
@Table
public class Customer {
@Id
@GeneratedValue
@Column(columnDefinition="serial")
private int id;
}
Run Code Online (Sandbox Code Playgroud)
看来,使用@Column(columnDefinition="serial")主键会自动将引用它的外键设置到NOT NULL数据库中.将列类型指定为时,这确实是预期的行为serial吗?在这种情况下,是否有解决方案来启用可为空的外键?
先感谢您.
nullable ×10
c# ×7
.net ×3
generics ×3
autoboxing ×1
datetime ×1
jboss7.x ×1
jpa-2.0 ×1
many-to-one ×1
reflection ×1
sql ×1
ssms ×1
typescript ×1