casting

Cast Java Object into Java Map<String,Object>

谁说我不能喝 提交于 2020-01-01 08:56:39
问题 I am using org.eclipse.jetty.util.ajax.JSON to parse JSON text. But the JSON.parse(string) method produces an Object and I need it as a Map. Internally it is an object of exactly the mentioned class. But how do you cast an Object into a Map without constructing a new one or getting the unchecked cast warning? Currently, I have found only one solution without the unchecked cast warning, but with constructing a new Map, which is actually of course not a casting at all. private Map<String,Object

Cast Java Object into Java Map<String,Object>

耗尽温柔 提交于 2020-01-01 08:56:29
问题 I am using org.eclipse.jetty.util.ajax.JSON to parse JSON text. But the JSON.parse(string) method produces an Object and I need it as a Map. Internally it is an object of exactly the mentioned class. But how do you cast an Object into a Map without constructing a new one or getting the unchecked cast warning? Currently, I have found only one solution without the unchecked cast warning, but with constructing a new Map, which is actually of course not a casting at all. private Map<String,Object

Cast object to IEnumerable<object>?

放肆的年华 提交于 2020-01-01 08:34:13
问题 How can I cast an object to IEnumerable<object> ? I know that the object implements IEnumerable<object> but I don't know what type it is. It could be an array, a List<T> , or whatever. A simple test case I'm trying to get working: static void Main(string[] args) { object arr = new[] { 1, 2, 3, 4, 5 }; foreach (var item in arr as IEnumerable<object>) Console.WriteLine(item); Console.ReadLine(); } 回答1: It's hard to answer this without a concrete use-case, but you may find it sufficient to

Unable to cast object of type 'System.Object[]' to 'MyObject[]', what gives?

回眸只為那壹抹淺笑 提交于 2020-01-01 08:34:09
问题 Scenario: I'm currently writing a layer to abstract 3 similar webservices into one useable class. Each webservice exposes a set of objects that share commonality. I have created a set of intermediary objects which exploit the commonality. However in my layer I need to convert between the web service objects and my objects. I've used reflection to create the appropriate type at run time before I make the call to the web service like so: public static object[] CreateProperties(Type type,

Cast object to IEnumerable<object>?

时光总嘲笑我的痴心妄想 提交于 2020-01-01 08:34:02
问题 How can I cast an object to IEnumerable<object> ? I know that the object implements IEnumerable<object> but I don't know what type it is. It could be an array, a List<T> , or whatever. A simple test case I'm trying to get working: static void Main(string[] args) { object arr = new[] { 1, 2, 3, 4, 5 }; foreach (var item in arr as IEnumerable<object>) Console.WriteLine(item); Console.ReadLine(); } 回答1: It's hard to answer this without a concrete use-case, but you may find it sufficient to

How can I determine if an implicit cast exists in C#?

…衆ロ難τιáo~ 提交于 2020-01-01 07:31:08
问题 I have two types, T and U, and I want to know whether an implicit cast operator is defined from T to U. I'm aware of the existence of IsAssignableFrom, and this is not what I'm looking for, as it doesn't deal with implicit casts. A bit of googling led me to this solution, but in the author's own words this is ugly code (it tries to cast implicitly and returns false if there's an exception, true otherwise...) It seems testing for the existence of an op_Implicit method with the correct

UnsafeMutablePointer<Void> to Concrete Object Type

一世执手 提交于 2020-01-01 05:34:11
问题 How can I cast from UnsafeMutablePointer<Void> to Concrete Object Type I've created a KVO observer and passed a custom class as context just like this class Info : NSObject { } class Foo : NSObject { dynamic var property = "" } var info = Info() class Observing : NSObject { func observe1(object: NSObject) { object.addObserver(self, forKeyPath: "property", options: .New, context: &info) } override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject :

Overloading c++ typecasting (functions)

蹲街弑〆低调 提交于 2020-01-01 04:59:05
问题 Using C++ style typecastings (all 4) look exactly like some function template . e.g. template<typename TO, typename FROM> TO dynamic_cast (FROM p); will be used as, dynamic_cast<Derived*>(p); // p is Base* Why is it not allowed to overload them by language standard for custom usage ? (like we can overload the keywords like new/delete or other operators ) 回答1: Why is it not allowed to overload them by language standard for custom usage? I suppose that's because the standard committee, when

Are dynamic_casts safe to remove in production code?

两盒软妹~` 提交于 2020-01-01 04:46:45
问题 dynamic_cast s are slower, but they are safer than static_cast s (when used with object hierarchies, of course). My question is, after I've ensured in my debug code that all (dynamic) casts are correct, is there any reason for me not to change them to static_cast s? I plan on doing this with the following construct. (Btw, can you think of a better name than assert_cast ? Maybe debug_cast ?) #if defined(NDEBUG) template<typename T, typename U> T assert_cast(U other) { return static_cast<T>

PostgreSQL create index on cast from string to date

浪尽此生 提交于 2020-01-01 04:22:23
问题 I'm trying to create an index on the cast of a varchar column to date. I'm doing something like this: CREATE INDEX date_index ON table_name (CAST(varchar_column AS DATE)); I'm getting the error: functions in index expression must be marked IMMUTABLE But I don't get why, the cast to date doesn't depends on the timezone or something like that (which makes a cast to timestamp with time zone give this error). Any help? 回答1: Your first error was to store a date as a varchar column. You should not