在我的代码中使用我写的反射
if (f.FieldType.IsAssignableFrom("".GetType()))
Run Code Online (Sandbox Code Playgroud)
我有一个隐式转换为字符串的类.但是上面的if语句并没有抓住它.我如何使用隐式字符串转换来进行反射/上述if语句捕获字符串和类?而不是专门的字符串和我知道的每个类?
我正在创建自己的类型来表示css值(例如像素,例如12px).为了能够添加/减去/乘以/ ...我的类型和整数我已经在int中定义了两个隐式运算符.一切都很好,除了一件事..如果我写:
CssUnitBase c1 = 10;
Console.WriteLine(c1);
Run Code Online (Sandbox Code Playgroud)
我得到"10"而不是"10px" - 使用隐式转换为int而不是ToString()方法.我怎么能防止这种情况?
通过以下对C#中的空合并运算符(??)的理解.
int? input = -10;
int result = input ?? 10;//Case - I
//is same as:
int result = input == null? input : 10; // Case - II
Run Code Online (Sandbox Code Playgroud)
而根据定义和用法,案例I和案例II是相同的.
令人惊讶的是,在Case-I编译器中能够隐式转换int吗?to case而在Case-II中它显示错误:'错误1无法隐式转换类型'int?' 'int'"
关于null-coalescing运算符,我错过了什么?
谢谢你的关注.
有一节课:
public class Date
{
private DateTime _dateTime;
public Date(DateTime dateTime)
{
_dateTime = dateTime;
}
public static implicit operator DateTime(Date d)
{
if (d == null)
return default(DateTime);
return d._dateTime;
}
public static implicit operator Date(DateTime dt)
{
return new Date(dt);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,这段代码工作正常:
Date d = DateTime.Now;
DateTime dt=new Date(DateTime.Now);
Run Code Online (Sandbox Code Playgroud)
但是此代码不起作用并抛出InvalidCastException"指定的强制转换无效.":
Date d = DateTime.Now;
var obj = (Object)d;
DateTime dt = (DateTime)obj;
Run Code Online (Sandbox Code Playgroud)
对我来说,这是预期的行为,但有没有任何黑客可以让这个特定的代码工作?(没有施放对象到日期)
(试图找到一个总结问题的标题可能是一项非常艰巨的任务!)
我有以下类与一些重载方法,产生一个调用歧义编译器错误:
public class MyClass
{
public static void OverloadedMethod(MyClass l) { }
public static void OverloadedMethod(MyCastableClass l) { }
//Try commenting this out separately from the next implicit operator.
//Comment out the resulting offending casts in Test() as well.
public static implicit operator MyCastableClass(MyClass l)
{
return new MyCastableClass();
}
//Try commenting this out separately from the previous implicit operator.
//Comment out the resulting offending casts in Test() as well.
public static implicit operator MyClass(MyCastableClass l)
{
return new …Run Code Online (Sandbox Code Playgroud) short s;
s = (EitherTrueOrFalse()) ? 0 : 1;
Run Code Online (Sandbox Code Playgroud)
这失败了:
错误CS0266:无法将类型'int'隐式转换为'short'.存在显式转换(您是否错过了演员?)
任何人都可以解释为什么会这样吗?我能想到的唯一一件事就是编译器没有查看第二个值并且不知道两者之间的范围,在我编写类似的情况下
short s;
s = (EitherTrueOrFalse()) ? 0 : 65000;
Run Code Online (Sandbox Code Playgroud)
正确?唯一的解决方案是丑陋的演员?
此外,似乎C#没有短类型的类型后缀.这是一个非常严重的监督IMO.否则,这将是一个解决方案......
如果我创建一个指向基类的成员,我通常可以将它转换为指向派生的成员,但是在下面的Buzz模板中使用时,第一个模板参数会影响第二个模板参数.我是在与编译器错误斗争还是标准真的要求这不起作用?
struct Foo
{
int x;
};
struct Bar : public Foo
{
};
template<class T, int T::* z>
struct Buzz
{
};
static int Bar::* const workaround = &Foo::x;
int main()
{
// This works. Downcasting of pointer to members in general is fine.
int Bar::* y = &Foo::x;
// But this doesn't, at least in G++ 4.2 or Sun C++ 5.9. Why not?
// Error: could not convert template argument '&Foo::x' to 'int Bar::*'
Buzz<Bar, &Foo::x> test;
// …Run Code Online (Sandbox Code Playgroud) 我了解到这i+=2是缺点i=i+2.但现在我怀疑它.对于以下代码,上述知识没有任何好处:
byte b=0;B = B + 2; //错误:必需字节,找到int
上面的代码是合理的,因为2是int类型和表达式返回int值.
但是,以下代码运行正常:
byte b=0; b+=2; //b stores 2 after += operation
这迫使我怀疑+=短手操作员比我所知道的更多.请赐教.
我们创建Id32并Id64构造了包装来自DB的整数和长值,因此可以通过Json转换器(使用专用的自定义转换器)将它们显式处理为ID.
问题是我们从一个Dictionary<string, object>实际上是类似DataRow对象读取这些数据,其中string部分是列的名称,object部分是值.
所以在我们使用此代码读取值之前:
int myVal = (int)row["COLUMN"]
Run Code Online (Sandbox Code Playgroud)
我们希望此代码在这些更改后也能继续工作.
但由于row["COLUMN"]是object(@ compile-time)隐式转换失败,即使它实际上是Id32(@运行时).
以下显然有效:
int myVal = (Id32)row["COLUMN"]
Run Code Online (Sandbox Code Playgroud)
但有没有办法解决这个问题而不修改读取值的代码?
这是结构代码:
public struct Id32
{
public readonly int Value;
public Id32(int id) { this.Value = id; }
public static implicit operator int(Id32 id) { return id.Value; }
public static implicit operator Id32(int id) { return new Id32(id); }
}
Run Code Online (Sandbox Code Playgroud) implicit-cast ×10
c# ×7
.net ×2
c++ ×2
ambiguous ×1
coding-style ×1
downcast ×1
java ×1
nullable ×1
overloading ×1
reflection ×1
shorthand ×1
templates ×1