Tag: 铸造

我可以从一个genericstypes转换为C#中的枚举吗?

我正在写一个实用程序函数,从数据库中获取一个整数,并返回一个types的枚举到应用程序。 这是我试图做的(注意我传入一个数据读取器和列名称,而不是在我的真实函数int ): public static T GetEnum<T>(int enumAsInt) { Type enumType = typeof(T); Enum value = (Enum)Enum.ToObject(enumType, enumAsInt); if (Enum.IsDefined(enumType, value) == false) { throw new NotSupportedException("Unable to convert value from database to the type: " + enumType.ToString()); } return (T)value; } 但是,我不会让(T)value 无法将types“System.Enum”转换为“T”。 另外我读了很多有关使用Enum.IsDefined的综合评论。 性能明智,这听起来很差。 我还能如何保证有效的价值?

为什么我们将一个父引用分配给Java中的子对象?

我在问一个很简单的问题,但是我有点困惑。 假设我有一个class级Parent : public class Parent { int name; } 并有另一个类Child.java : public class Child extends Parent{ int salary; } 最后是我的Main.java类 public class Main { public static void main(String[] args) { Parent parent = new Child(); parent.name= "abcd"; } } 如果我做一个孩子的对象 Child child = new Child(): 然后, child对象可以访问name and salaryvariables。 我的问题是: Parent parent = new […]

在c ++中将非const转换为const

我知道你可以使用const_cast将一个const转换为非const 。 但是,如果你想将非const为const你应该使用什么?

static_cast和reinterpret_cast有什么区别?

可能重复: 什么时候应该使用static_cast,dynamic_cast和reinterpret_cast? 我在c ++中使用c函数,在c中作为voidtypesparameter passing的结构直接存储在相同的结构types中。 例如在C. void getdata(void *data){ Testitem *ti=data;//Testitem is of struct type. } 在c ++中做同样的事情我使用static_cast: void foo::getdata(void *data){ Testitem *ti = static_cast<Testitem*>(data); } 当我使用reinterpret_cast它做同样的工作,铸造结构 当我使用Testitem *it=(Testitem *)data; 这也是同样的事情。 但是如何利用三者来影响结构呢?

为什么编译器让我在C#中将特定的types转换为null?

考虑这个代码: var str = (string)null; 当写代码这是我的IL代码: IL_0001: ldnull IL有Cast操作符,但是: var test = (string) new Object(); IL代码是: IL_0008: castclass [mscorlib]System.String 所以将null强制转换为string被忽略。 为什么编译器让我把null成特定的types?

覆盖自定义类的bool()

所有我想要的是为bool(myInstance)返回False(和myInstance评估为False时,如条件,如果/ / /和。我知道如何覆盖>,<,=) 我试过这个: class test: def __bool__(self): return False myInst = test() print bool(myInst) #prints "True" print myInst.__bool__() #prints "False" 有什么build议么? (我正在使用Python 2.6)

C#是'types检查结构奇怪的.NET 4.0 x86优化行为

更新:我已经提交了Microsoft Connect的错误报告 ,请投票! 更新2:微软已经将bug报告标记为已修复 由微软于18/08/2010在17:25发布 该错误将在未来版本的运行时中修复。 恐怕现在判断是否会在服务包或下一个主要版本中还为时过早。 由于升级到VS2010,我得到一些非常奇怪的行为与“is”关键字。 下面的程序(test.cs)在debugging模式(对于x86)编译时输出True,在(对于x86)优化编译时输出False。 编译x64或AnyCPU中的所有组合会给出预期结果,即True。 在.NET 3.5下编译的所有组合都会给出预期的结果,True。 我正在使用下面的batch file(runtest.bat)来编译和testing使用编译器.NET框架的各种组合的代码。 有没有人在.NET 4.0下看到过这种types的问题? 当运行runtests.bat时,其他人是否在计算机上看到和我一样的行为? @ $ @#$? 有没有解决这个问题? test.cs中 using System; public class Program { public static bool IsGuid(object item) { return item is Guid; } public static void Main() { Console.Write(IsGuid(Guid.NewGuid())); } } runtest.bat @echo off rem Usage: rem runtest — […]

SQL中的双冒号(:)表示法

已经拿起某人的代码,这是一个where子句的一部分,任何人都知道双冒号指示什么? b.date_completed > a.dc::date + INTERVAL '1 DAY 7:20:00'

将Objective-C指针types'NSString *'转换为C指针types'CFStringRef'(又名'const struct __CFString *')需要一个桥接转换

将Objective-C程序转换为Objective-C ARC时,出现以下错误: "cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast " 代码如下: – (NSString *)_encodeString:(NSString *)string { NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)string, // this is line in error NULL, (CFStringRef)@";/?:@&=$+{}<>,", kCFStringEncodingUTF8); return [result autorelease]; } 什么是桥接演员?

将std :: __ cxx11 :: string转换为std :: string

我使用c + + 11,但也有一些没有configuration它的库,需要一些types转换。 特别是我需要一种方法来将std::__cxx11::string转换为常规的std::string ,但是使用谷歌search我找不到一个方法来做到这一点,把(string)放在前面不起作用。 如果我不转换我得到像这样的链接器错误: undefined reference to `H5::CompType::insertMember(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, H5::DataType const&) const'