casting

Is it possible to cast a list of reflected Types to their original strongly typed objects?

情到浓时终转凉″ 提交于 2019-12-11 09:16:55
问题 From the second line of the code below, I retrieve a List of Types. I would like to return this as a list of IBusinessObject. Is this possible? And if so, how would I go about doing this? public List<IBusinessObject> RetrieveAllBusinessObjects() { var businessObjectType= typeof(IBusinessObject); List<Type> implementationsOfBusinessObject = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(businessObjectType.IsAssignableFrom).ToList(); return ?; } 回答1: Here a

what's the runtime equivalent of c# 'bracketed' type cast

﹥>﹥吖頭↗ 提交于 2019-12-11 09:16:44
问题 suppose I have an enum [Flags] public enum E { zero = 0, one = 1 } then I can write E e; object o = 1; e = (E) o; and it will work. BUT if I try to do that at runtime, like (o as IConvertible).ToType(typeof(E), null) it will throw InvalidCastException. So, is there something that I can invoke at runtime, and it will convert from int32 to enum, in the same way as if I wrote a cast as above? 回答1: object o = 1; object z = Enum.ToObject(typeof(E), o); 回答2: How does the variable look like that you

Determining index from bsearch and lfind?

孤街浪徒 提交于 2019-12-11 08:26:14
问题 I'm trying to get the index of the element in the array after lfind and bsearch return the pointer to the element that it found. I have this so far: (char *) (found - cv->baseAddress); where found is the address of what the functions found, and the base address is the address of element 0. However, the compiler gives me this error: cvector.c:150:28: warning: pointer of type ‘void *’ used in subtraction cvector.c:150:4: warning: return makes integer from pointer without a cast What do I do?

Geom.ribbon in Gadfly (Julia) does not accept Int vectors as input

半世苍凉 提交于 2019-12-11 08:23:58
问题 So, I've been playing around with Gadfly in Julia and run into this issue. Geom.ribbon does't accept Int vectors as input for ymin and ymax when used in combination with Geom.smooth. I was wondering if this is a bug, is intended, or if I'm doing anything wrong and why? In [1]: x=[-10:10] y=[i^2 for i in -10:10] ymin = y-10 ymax = y+10; In [2]: plot(x=x, y=y, ymin=ymin, ymax=ymax, Geom.smooth, Geom.ribbon) Out[2]: `minvalmaxval` has no method matching minvalmaxval(::Int64, ::Float64, ::Int64,

pointer typecasting like c in python

故事扮演 提交于 2019-12-11 08:06:32
问题 I've a c code to type cast a string to an integer via pointer. char s[]="efgh"; int * p; p=(int *) s; printf("%d",*p); This gives me an output of: 1751606885 Which is a 32 bit integer. I'm analyzing a network packet in python and need the above functionality in python. I've a string s="efgh" and want the above in a 32 bit integer (from the byte level). How can I do it? 回答1: You can try struct.unpack: >>> import struct >>> >>> struct.unpack('<I', 'efgh') (1751606885,) >>> 来源: https:/

Cast Integer to Float using Bit Manipulation breaks on some integers in C

倖福魔咒の 提交于 2019-12-11 08:00:59
问题 Working on a class assignment, I'm trying to cast an integer to a float only using bit manipulations (limited to any integer/unsigned operations incl. ||, &&. also if, while). My code is working for most values, but some values are not generating the results I'm looking for. For example, if x is 0x807fffff, I get 0xceff0001, but the correct result should be 0xceff0000. I think I'm missing something with my mantissa and rounding, but can't quite pin it down. I've looked at some other threads

Java - How to create a Class<Map<Object,List<Object>>> object

雨燕双飞 提交于 2019-12-11 07:56:56
问题 I'm sorry if this is a noob question. I have a method which takes a Class object with the generic type as a parameter. I am able to do the following: Class cs = Map.class; but when I pass this I get an "NoSuchMethodException" since the method is declared: public void doSomething(Class<Map<Object, List<Object>>> theclass){ ... } I have tried to cast it like this: Class cs = (Class<Map<Object, List<Object>>>)(Class<?>)Map.class; but I still get the same exception. Is there a way to do this? I

typecast BOOL always returns false in iOS 8.1.1

让人想犯罪 __ 提交于 2019-12-11 07:56:36
问题 I save & retrive the BOOL value in a dictionary using the following methods. [userData setObject:@YES forKey:@"IS_AGENT"]; (BOOL) [userData objectForKey:@"IS_AGENT"]; In the above code while retrying the bool value from the dictionary I always get false in the following devices iPhone5s, iPhone5c, iPhone6, iPhone6+ which has iOS 8.1.1 and the same code works fine in iPod Touch which has the same iOS 8.1.1 After googled I came to know that we should not type cast BOOL like this because " BOOL

Cast or convert DD-MON-YYYY AM/PM to YYYY-MM-DD

六眼飞鱼酱① 提交于 2019-12-11 07:52:29
问题 I'm stuck in finding an answer on how to convert: 07-DEC-18 01.00.54.984000 PM to 2018-12-07 13.00.54.984000 I think the time of 01.00.54 PM is 13hours , 0 minutes and 54 seconds I have try to convert with 112 but still i can't find out how to cast or convert this. 回答1: Below is one method that converts the date and time portions separately, and then uses DATEADD to combine the results. This assumes the actual time precision is not greater than milliseconds. Note that you need to use

Cast specific types in variadic argument

时光总嘲笑我的痴心妄想 提交于 2019-12-11 07:35:11
问题 I have a template function that accepts variadic arguments. template<typename... Params> void foo(Params... p); I want to find all occurences of a given type ( const char* ) in Params to replace them with another type, that these values can be cast to (my own Path class with constructor Path(const char*) ). The idea is to have something like template<typename... Params> void foo(Params... p) { bar<convertCharPointerToPath<Params>...>(p...); } How can this conversion be done? 回答1: If you want