casting

Casting a class using reflection in java

给你一囗甜甜゛ 提交于 2019-12-13 18:30:26
问题 I have the following classes. abstract class A{} class B extends A{} class C extends A{} I need to create an object like this A a = new B(); I get the class name of the subclass at runtime. So, I need to use reflection to create an object. I have done this. Class<?> klass = Class.forName(className); Now I am not able to cast this class to the subclass. I want to achieve A a = klass.newInstance(); // returns Object I want the object to be casted into the subclass (either B or C , decided on

Generate Cast type dynamically in C#

左心房为你撑大大i 提交于 2019-12-13 17:27:06
问题 Please I have a class in c# whose main function is to return the types and objects as dictionary Over a service . Is it possible to cast the Object sents over the WCF service in the front end. I.e using reflection to get the type of an object from the types.ToString() and using the type to cast the objects. NB the Class that returns the dictionary and my frontend are in different projects so different Namespaces: Type repType = typeof(List <>).MakeGenericType(Type.GetType(EntityandTypes

Why does as instead of a cast work? [duplicate]

Deadly 提交于 2019-12-13 17:23:51
问题 This question already has answers here : Why does casting give CS0030, while “as” works? (3 answers) What is the difference between casting and using “as” in C#? (8 answers) Closed 2 years ago . I have two classes, where the first class references the second class. My question is, why in the second class, the line cl.container = this as ClassContainer<MyClass>; does work and the explicit cast cl.container = (ClassContainer<MyClass>)this; does not. class MyClass { public ClassContainer<MyClass

Generic Casting

…衆ロ難τιáo~ 提交于 2019-12-13 16:40:29
问题 I suspect the answer is no, but is it possible to do something like this in C#.NET (v2.0). class Converter<E> { public E Make(object o) { return o as E; } } If not, is it possible to check types like this: public bool IsType(object o, Type t) { return o is E; } I'm not certain about the terminology so it is rather hard to Google for me. But my guess is that these two problems are related. Any ideas? 回答1: You can cast o to E using the () Operator: class Converter<E> { public E Make(object o) {

Double Array to Object Array?

跟風遠走 提交于 2019-12-13 16:29:16
问题 I have following array: double[] Series = new double[] { 7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6 } Series debugging view : > Series --> [0] --> [1] --> ... --> [10] --> [11] When I write following: object[] object_array = new object[] { Series } object_array debugging view : (more than one level) > object_array --> [0] ----> [0] ----> [1] ----> ... ----> [10] ----> [11] I write following to prevent new level: object[] object_array = new object[Series.Length]; for

Why does a type conversion not work in Java [duplicate]

余生颓废 提交于 2019-12-13 15:41:28
问题 This question already has answers here : Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic? (17 answers) Closed 5 years ago . I`m wondering why this conversion is not working: ArrayList<Song> arrayList =new ArrayList<MediaItem>(); I may have to add that Song extends MediaItem. I think this conversion should work because Song has the ability to store all the information form MediaItem. So no information is lost. Does anyone have an explanation for me?

Cast a superclass to a subclass [duplicate]

China☆狼群 提交于 2019-12-13 15:12:22
问题 This question already has answers here : Java casting resulting in run-time error instead of compilation error (7 answers) Closed 6 years ago . Imagine a class Cottage extending Building and the code Building building = new Building(); Cottage cottage = (Cottage)building; Now, it totally makes sense that Building cannot be casted to Cottage considering the nature of java's inheritance, but what doesn't make sense (to me) is that this compiles. Why does it compile and then throw a runtime

Unable to cast COM object of type '…' to interface type '…' while using an ExeCOMServer

╄→尐↘猪︶ㄣ 提交于 2019-12-13 14:55:32
问题 I'm using this exe com server: https://cfx.svn.codeplex.com/svn/Visual%20Studio%202008/CSExeCOMServer/ExeCOMServer.cs my prog is a com app my com method which take another com object is void Init(AppsScriptRunningContext rc); in this method I try to read out an property and get this error Unable to cast COM object of type 'AppsScriptLib.AppsScriptRunningContextClass' to interface type 'AppsScriptLib.IAppsScriptRunningContext'. This operation failed because the QueryInterface call on the COM

dynamic casting?

南笙酒味 提交于 2019-12-13 14:16:13
问题 I need a way to cast an object to a type that is not know at compiler time. something like this: object obj; public (type of obj) Obj { get { return obj } set { obj = (type of obj)value; } } The only thing that is know is that obj is a value type. I doubt that something like this would be possible. Just checking to see if someone has a clever way of doing it. 回答1: This is not possible in C# 3.0, but if you could define a common interface that all of your objects implement, you could cast to

Which kind of cast is from Type* to void*?

元气小坏坏 提交于 2019-12-13 13:06:46
问题 In C++ for any data type I can do the following: Type* typedPointer = obtain(); void* voidPointer = typedPointer; which cast is performed when I assign Type* to void* ? Is this the same as Type* typedPointer = obtain(); void* voidPointer = reinterpret_cast<void*>( typedPointer ); or is it some other cast? 回答1: It is a standard pointer conversion. Since it is a standard conversion, it doesn't require any explicit cast. If you want to reproduce the behavior of that conversion with an explicit