Questions tagged [gettype]

1

votes
1

answer
990

Views

Getting entities of a certain type using entity frameworks return error - LINQ to Entities does not recognize the method 'System.Type GetType()'

I am trying to filter out entities of a certain type. I am using Entity Framework, and I have a parent entity, and various children that inherit from the parent entity. I am trying to filter out one of these children. For example, I have the following structure: Address : Entity PostCode : Address...
Karan
1

votes
8

answer
220

Views

How to extract an object name

i've got a class filled with lists of subclasses: public class ClassOfKb { public List KbDatas {get;set;} public List KbProducts {get;set} } public class Data { public Guid ID {get;set;} public byte[] data {get;set;} public string Name {get;set;} } public class Product { public Guid ID {get;set;} pu...
Raymond Osterbrink
1

votes
3

answer
63

Views

Common integer representation over variable casting?

For a while i've been using: gettype(); to dynamically get the variable cast type, eg: function Type ($Value = null){ return gettype($Value); } echo Type(array()); // Echo array echo Type (1); // Echo integer echo Type(1.2); // echo doubble Whereas, i'm looking for a method to have a common and kno...
Sophie Mackeral
2

votes
1

answer
131

Views

What does (type1)+(type2) means using GetType().ToString()?

I'm writing a little Console app to test some basic MS ProjectServer interop. I can connect to the server with no problems, and I was trying to refactor one of my for (int i = 0; i < projectDataSet.Project.Count; i++) loops for a foreach (var project in projectDataSet.Project) one. When I do the fo...
Bruno Saboia
16

votes
5

answer
36.5k

Views

How to get name of a class property?

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 sav...
Jeff
18

votes
4

answer
10.3k

Views

Why would System.Type.GetType(“Xyz”) return null if typeof(Xyz) exists?

I have come across a strange behaviour in my (huge) .NET 4 project. At some point in the code, I am referring to a fully qualified type, say: System.Type type = typeof (Foo.Bar.Xyz); later on, I do this: System.Type type = System.Type.GetType ("Foo.Bar.Xyz"); and I get back null. I cannot make sense...
Pierre Arnaud
1

votes
1

answer
1.4k

Views

Type.GetType() visibility issue between projects (Visual C#)

I ran into a peculiarish problem. I am working on a dynamic factory project and my intention is to be able to create new objects based on an XML file. My problem is this: I have a separate project for the base classes for the factory where I have public abstract class DynamicContentFactory where T:...
Sami Anttila
1

votes
2

answer
217

Views

How to tell if ParameterInfo type is a collection?

Is there a way to check whether ParameterInfo is a Collection? I have tried this: ConstructorInfo[] constructorInfos = typeof(T).GetConstructors(); ConstructorInfo constructorInfo = constructorInfos[0]; ParameterInfo[] paramsVar = constructorInfo.GetParameters(); IEnumerable collectionParams = param...
DevDave
20

votes
2

answer
862

Views

Unexpected value of System.Type.FullName

I recently needed to build C# specific name (which must always include global:: specifier) for an arbitrary type and have come accross following issue: // 1 - value: System.String[,,,][,,][,] string unexpectedFullName = typeof( string[,][,,][,,,] ).FullName; // 2 - value: System.String[,][,,][...
Rest Wing
2

votes
5

answer
3.3k

Views

C# Generic T Class TypeOf, is this possible?

I have a class that is generic. Class and depending in the switch statement in the calling code it can be class class class The the method that returns this returns it as an object because the calling code has no idea what it is until after it is set. Is there a way to do this once I get the object...
nitefrog
2

votes
2

answer
1.4k

Views

Type.getType() can't find the class

I have a lot of static methods in a class, I want to get if a certain method is in the class X, and if it is, I want to invoke it. I checked with this: if (Type.GetType("Homework.Homework.Functions").GetMethod(methodName) == null) { Console.WriteLine("No such method.\nPress any key to restart the pr...
shoham
2

votes
4

answer
952

Views

Why does a GetType on a string-property result in a NullReferenceException?

When I call a GetType on a int- or a DateTime-property, I get the expected results, but on a string-property, I get a NullReferenceException (?) : private int PropInt { get; set; } private DateTime PropDate { get; set; } private string propString { get; set; } WriteLine(PropInt.GetTy...
11

votes
3

answer
33.4k

Views

How to get class type by its class name?

namespace Myspace { public class MyClass { } } //This class is in another file. using Myspace; static void Main(string[] args) { Regex regexViewModelKey = new Regex(RegularExpr.ViewModelKeyPattern); string viewModel = regexViewModelKey.Match(match.Value).Value; //Now, vieModel is a string, and its v...
Huan Fu
4

votes
7

answer
295

Views

Is there a way to get the type of the objects stored in a generic list?

Given a generic list of type List how do I find type T? I suppose if the list is populated I could take listInstance[0].GetType() but that seems a bit hackish. Edit: For context, I want to populate a DataTable with columns based on the Properties of an object. Where an object property is a generic l...
Phil Gan
2

votes
3

answer
6.5k

Views

c# casting to type gotten from typename as string

I want to work around the fact that my WCF servicelayer can not handle a generic method like this: public void SaveOrUpdateDomainObject(T domainObject) { domainRoot.SaveDomainObject(domainObject); } so I built this workaround method instead public void SaveOrUpdateDomainObject(object doma...
2

votes
0

answer
232

Views

How to get Type information when ReflectionTypeLoadException is thrown from Assembly.GetType() [closed]

OK, to start off, I have read both How to prevent ReflectionTypeLoadException when calling Assembly.GetTypes() and http://haacked.com/archive/2012/07/23/get-all-types-in-an-assembly.aspx but my problem is a bit more complicated. My goal is simply to see if an assembly contains classes (or types) t...
Branden Boucher
2

votes
2

answer
235

Views

Error creating runtime object of type System.Drawing.Point

I want to create a object of type System.Drawing.Point at runtime, I tried to use the code below: String typename = "System.Drawing.Point"; Type tp = Type.GetType(typename); Object instance = Activator.CreateInstance(tp); But I always get tp == null. But, for example if I use System.Double everythin...
Rui Sebastião
2

votes
1

answer
3.4k

Views

How to get a Type from an object in another DLL?

I've got an object called "Communication" that has a method to "CreatePdfFromTemplate". This method is going to be called from a Windows Service that has a SqlDependancy on a table that will notify when a new row is added by a method on a website. Into my method, I pass a list of custom objects that...
yellowbrickcode
2

votes
2

answer
921

Views

Get Enum List via GetType

I have code that gives a list of all possible values for any given enum i bound it pretty often to dropdownlists in my webpages now im trying to make a usercontrol which accepts the type name as a parameter, which in turn calls the code to create the value list as my sub expects a type parameter S...
1

votes
3

answer
632

Views

C#: Parse different classes as object and display property values the same way

I have a couple of classes in c# console application. public class Cars:List {} and: public class Drivers:List {} The goal is to render console-output always the same manner, doesn't matter if cars or drivers are parsed to rendering-method. So the parameter is of type object. Of course the properti...
Jimbo2015
3

votes
5

answer
838

Views

Get the parent class of a null object (C# Reflection)

How would I get the parent class of an object that has a value of null? For example... ClassA contains int? i which is not set to any value when the class is created. Then in some other place in the code I want to pass in i as a parameter to some function. Using i as the only info, I want to be able...
Nick
2

votes
1

answer
2.1k

Views

.NET reflection - Get Declaring class type from instance property

Is it possible to get the type of a class from a property instance I tried the following var model = new MyModel("SomeValueForMyProperty") Type declaringType = model.MyProperty.GetType().DeclaringType But the result is always not for both DeclaringType and ReflectedType
ricardo
3

votes
3

answer
1.1k

Views

Why does the gettype() say it's a double but var_dump() says float?

Why does the gettype() say it's a double but var_dump() says float? $number = 1234567890123456789; echo "Number: {$number} is a ". gettype($number) . "\n"; var_dump($number); Response: Number: 1.23456789012E+18 is a double float(1.23456789012E+18)
Phill Pafford
28

votes
4

answer
6.4k

Views

Why GetType returns System.Int32 instead of Nullable<Int32>? [duplicate]

This question already has an answer here: Nullable type is not a nullable type? 4 answers Why is the output of this snippet System.Int32 instead of Nullable? int? x = 5; Console.WriteLine(x.GetType());
Arsen Mkrtchyan
4

votes
2

answer
11.8k

Views

How return the type of a System.__COMObject in System.Type in C#

I'm doing a program and I want to do a Reflection, but for this, I need an Object of the Type class, right? to use the .GetProperties() method... So I tryed this: Type typeName = simObjects.getType(); But the .GetType() is returning "System.__COMObject". And this is not helpfull. The same happens w...
Jovita
28

votes
12

answer
16.4k

Views

.NET : How do you get the Type of a null object?

I have a method with an out parameter that tries to do a type conversion. Basically: public void GetParameterValue(out object destination) { object paramVal = "I want to return this. could be any type, not just string."; destination = null; // default out param to null destination = Convert.ChangeTy...
CodingWithSpike
1

votes
4

answer
1.1k

Views

ASP.NET - cant GetType() of MasterPage

I have a MasterPage and get its type as follows: alt text http://www.yart.com.au/stackoverflow/masterpage.png No problem, this works Now, when I create an aspx page and try the same thing I get null: alt text http://www.yart.com.au/stackoverflow/page.png Why? How can I get this to work? NOTE The ans...
Petras
6

votes
3

answer
8.2k

Views

Using GetCurrentMethod in (supposedly) high-performance code

For logging purposes, some methods in our application include the following line: Dim Log As ILog = GetLog(Reflection.MethodBase.GetCurrentMethod().DeclaringType) I have what might be described as an irrational fear of reflection, which I try to keep in check. However, calls like this in methods tha...
Dan Tao
2

votes
1

answer
71

Views

How can i get the system type of a value i get from a form/string?

i have never tried something like this so i need your help. i have the following code: public int AddChannel(NameValueCollection FormValues) { string Keys = string.Join(",",FormValues.AllKeys); string Values = string.Join(",", FormValues.AllKeys.Select(key => String.Format("\"{0}\"", HttpContext.Cur...
Dementic
3

votes
3

answer
2.5k

Views

How to convert simple_html_dom object back to string?

I have used PHP Simple HTML DOM Parser to first convert an HTML string to DOM object by str_get_html() method of simple_html_dom.php $summary = str_get_html($html_string); Then I extracted an object from the $summary by foreach ($summary->find('img') as $img) { $image = $img; break; } Now I needed...
Solace
3

votes
2

answer
2k

Views

Getting the abstract class type from which a class derives

In .NET using the GetType function returns the concrete class type of the object. The problem is that i don't know what the type is going to be until runtime, but i do know from which abstract class its derives ( I am using abstract factories to create the adequate class). How can i get the actual a...
ak3nat0n
3

votes
2

answer
950

Views

Powershell: GetType() to print all type information, but a lot are missing?

I tried to use GetType() to print all type information, but seems much information is missing? I have such input in PS 4.0.(See below) I tried to use "GetType().GetMethods()" to list all methods. But I did not see "GetMethods" itself in the result list. I also used "GetProperties()" to list all pr...
vik santata
2

votes
1

answer
66

Views

How to create expression at run-time

I have a class contains 4 properties: class MyClass { public int i{get;set;} public double d{get;set;} public string s{get;set;} public char c{get;set;} } And a list of this class: var lstSource = new List { new MyClass {i = 1, d = 1.2, s = "s1", c = '1'}, new MyClass {i = 2, d = 2.2, s = "s2", c =...
Merta
23

votes
5

answer
17.4k

Views

C# Reflection: How to get the type of a Nullable<int>?

What I want to do is something like this: switch( myObject.GetType().GetProperty( "id") ) { case ??: // when Nullable, do this case ??: // when string, do this case ??: // when Nullable, do this What path under object.GetType() would have the string name of the datatype that I could compare using a...
Zachary Scott
2

votes
2

answer
2.3k

Views

C# Set Property in unknown object

I have to set a property inside an unknown object. The structure looks like this: ObjA.ObjB().ObjC.PropA = propValue; ObjA is from a referenced class. ObjB() is of type object and therefore ObjC is unknown. I thought about using Reflection but don't know how to use it correctly in this case. object...
SwissEngineer
2

votes
3

answer
361

Views

Is there a guranteed uniqueness for GetType().GetHashCode()?

Let's say I have a few classes: MyClass1() {} MyChild1() : MyClass1 {} MyChild2() : MyClass2 {} MyGrandchild1() : MyChild2 {} etc. I know that GetHashCode() by itself, does not guarantee uniqueness between any two different Objects, but I'm interested does that apply for any two Types as well? i.e.:...
avance70
1

votes
2

answer
59

Views

Reflection to get and use class properties

I am trying to update a linked list from a datagridview using reflection so I don't have to write a line of code for each property. The class: public class clsUnderlying { public int UnderlyingID { get; set; } public string Symbol { get; set; } public double RiskFreeRate { get; set; } public double...
Zeus
2

votes
2

answer
280

Views

What is a better way to check that a given object is a particular value type? [closed]

Below are the 2 commonly used approaches to check before unbox. myObject.GetType() == typeof(MyValueType) IL_0001: callvirt System.Object.GetType IL_0006: ldtoken UserQuery.MyValueType IL_000B: call System.Type.GetTypeFromHandle IL_0010: call System.Type.op_Equality myObject...
colinfang
7

votes
3

answer
6.5k

Views

How to Identify type of a variable

How do i properly identify a type of variable in c++. I tried this to identify a type of variable : int a = 5; std::cout
amanuel2
2

votes
1

answer
92

Views

How to parse source code fragment to System.Type

I have a set of strings like this: System.Int32 string bool[] List Dictionary I would like to test if those strings are actually source code representations of valid types. I'm in an environment, that doesn't support Roslyn and incorporating any sort of parser would be difficult. This is why I've tr...
Xarbrough

View additional questions