gettype

C# “Object reference not set to an instance of an object” Get Type / Get Property [duplicate]

China☆狼群 提交于 2019-12-12 03:37:44
问题 This question already has answers here : What is a NullReferenceException, and how do I fix it? (29 answers) Closed 3 years ago . Hi guys I'm receiving an error: "Object reference not set to an instance of an object". I'm not quite sure why... Here's the code: public void LoadUserContacts(ListBox FriendsLb) { FriendsLb.DisplayMember = "Display"; var query = from o in Globals.DB.Friends where o.UserEmail == Properties.Settings.Default.Email select new { FirstName = o.FirstName, LastName = o

PHP strcmp is not returning the correct answer with session variable

好久不见. 提交于 2019-12-11 23:36:30
问题 I have a session variable $_SESSION['condition'], I know it is set because when I entered this: echo $_SESSION['condition']." = "."Below Average"; It returns: Below Average = Below Average When I do a gettype() on the session variable it returns type "string". Yet when I do strcmp() it returns: -34 I have also tried an IF statement with == rather than strcmp testing for equality AND an IF statement casting them both as strings and testing if they are equal with no luck. Any reason why this

How to get underlying type from typed DataSet

我与影子孤独终老i 提交于 2019-12-11 13:41:58
问题 I've been searching a lot but I haven't found anything about this question. I'm making a log of my app and I'm printing types of variables and their values. I want to do the same for every object I receive as parameter, and for every object I return too. So I'm returning a typed dataset (MyDataSet that's defined as MyDataSetType e.g.) but I can't retrieve MyDataSetType name. I have a method that given a dataset, returns a string with all the content. Something like this: string

GetType() and Typeof() in C#

百般思念 提交于 2019-12-09 03:43:30
问题 itemVal = "0"; res = int.TryParse(itemVal, out num); if ((res == true) && (num.GetType() == typeof(byte))) return true; else return false; // goes here when I debugging. Why num.GetType() == typeof(byte) does not return true ? 回答1: Because num is an int , not a byte . GetType() gets the System.Type of the object at runtime. In this case, it's the same as typeof(int) , since num is an int . typeof() gets the System.Type object of a type at compile-time. Your comment indicates you're trying to

C# How to Initialize Generic class with object of type “Type”

杀马特。学长 韩版系。学妹 提交于 2019-12-07 17:09:20
问题 I recently had this problem. doSomething(typeof(int)); doSomething(typeof(MyClassA)); doSomething(typeof(MyClassB)); public void doSomething(Type _type) { var myGenObj = new MyGenericClass<_type>(); // Error. Really I'd want MyGenericClass<int>, MyGenericClass<MyClassA>, etc depending on what's passed in. myGenObj.doSomeGenStuff(); // more stuff... } I think that this can be done with reflection somehow.. Possibly there's an easier way. I've been somewhat confused on how Type works vs Classes

cursor.getType() and CursorIndexOutOfBoundsException exception

匆匆过客 提交于 2019-12-07 09:33:40
问题 There is a thing that I can not understand about Cursor.getType() , can anyone explain why do I get this stupid exception when I want to get columns type if cursor has no record but there are columns? I mean, if cursor has record there is no problem, I can use getType method to get columns type without any problem but if there is no any record it throws this exception out. The question is why must I need records to get columns type? Why just knowing columns name is not enough to get types of

C# How to Initialize Generic class with object of type “Type”

前提是你 提交于 2019-12-06 03:19:09
I recently had this problem. doSomething(typeof(int)); doSomething(typeof(MyClassA)); doSomething(typeof(MyClassB)); public void doSomething(Type _type) { var myGenObj = new MyGenericClass<_type>(); // Error. Really I'd want MyGenericClass<int>, MyGenericClass<MyClassA>, etc depending on what's passed in. myGenObj.doSomeGenStuff(); // more stuff... } I think that this can be done with reflection somehow.. Possibly there's an easier way. I've been somewhat confused on how Type works vs Classes under the covers. Anyways thanks for any help. Thanks. You want Type.MakeGenericType and then

C# — how does one access a class' static member, given an instance of that class?

≯℡__Kan透↙ 提交于 2019-12-04 19:09:31
问题 In C#, suppose you have an object (say, myObject ) that is an instance of class MyClass . Using myObject only, how would you access a static member of MyClass ? class MyClass { public static int i = 123 ; } class MainClass { public static void Main() { MyClass myObject = new MyClass() ; myObject.GetType().i = 456 ; // something like this is desired, // but erroneous } } 回答1: You'd have to use reflection: Type type = myObject.GetType(); FieldInfo field = type.GetField("i", BindingFlags.Public

How to get name of a class property?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 14:59:14
问题 Is there anyway I can get the name of class property IntProperty ? public class ClassName { public static int IntProperty { get { return 0; } } } //something like below but I want to get the string of "IntProperty" ClassName.IntProperty.GetType().Name Basically what I want to do is to dynamically save property name string into the database, and later on retrieve it from the database and invoke the property dynamically. Seems like what I am looking for is similar to duck typing I think. Thanks

C# — how does one access a class' static member, given an instance of that class?

邮差的信 提交于 2019-12-03 12:36:22
In C#, suppose you have an object (say, myObject ) that is an instance of class MyClass . Using myObject only, how would you access a static member of MyClass ? class MyClass { public static int i = 123 ; } class MainClass { public static void Main() { MyClass myObject = new MyClass() ; myObject.GetType().i = 456 ; // something like this is desired, // but erroneous } } You'd have to use reflection: Type type = myObject.GetType(); FieldInfo field = type.GetField("i", BindingFlags.Public | BindingFlags.Static); int value = (int) field.GetValue(null); I'd generally try to avoid doing this though