gettype

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 dyanmically save property name string into dat...

Use BuildManager.GetType to look through assemblies in WPF

I have existing code for an ASP .NET application that uses reflection to load dataproviders. I would like to re-use this code in a WPF application but it appears that BuildManager.GetType only looks through top level assemblies if the app isn't ASP .NET. Does anyone know how to get around this limitation? The following code throws an ex...

C# 'is' operator performance.

I have a program that requires fast performance. Within one of its inner loops, I need to test the type of an object to see whether it inherits from a certain interface. One way to do this would be with the CLR's built-in type-checking functionality. The most elegant method there probably being the 'is' keyword: if (obj is ISpecialTy...

ASP.NET - cant GetType() of MasterPage

I have a MasterPage and get its type as follows: No problem, this works Now, when I create an aspx page and try the same thing I get null: Why? How can I get this to work? NOTE The answers below say I need to reference an Assembly. But how do I do that when I am running this application as an ASP.NET website - there are on comp...

Need help converting C# to vb. "array initializer is missing 1 elements"

Hello people, I am trying to convet the following code from C# to Vb using 3.5 framework. Here is the code in C# that I am having trouble with. MethodInfo mi = typeof(Page).GetMethod("LoadControl", new Type[2] { typeof(Type), typeof(object[]) }); I thought it would be like this in VB; Dim mi As MethodInfo = GetType(Page).GetMethod("...

(C#) why does Visual Studio say it's an object while GetType says it's a Func<object> ?

Hi, C# newbie question here. The following code (taken from the book "C# From Novice to Professional" by Christian Gross, Apress) gives an error: worksheet.Add("C3", CellFactories.DoAdd(worksheet["A2"], worksheet["B1"])); The reason is that the method DoAdd() does not accept the given arguments. public static Func<object> DoAdd(Func...

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

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(...

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>(T domainObject) { domainRoot.SaveDomainObject<T>(domainObject); } so I built this workaround method instead public void SaveOrUpdateDomainObject(object domainObject, string typeName) { ...

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 that are executed potentially a hundred...

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.GetType().ToString()); // Result : Syst...

asp.net, gettype() and fully qualified class names

hey guys, I've read through a few other threads on here, though none of them really explain how to resolve my issue. I have a web application with the following page (code behind) namespace Company.Web.UI.Content { public partial class Home_LoggedOut : Company.Web.UI.CompanyPage { string _myType = this.GetType().FullName.Repla...

How to convert the current class name of asp.net usercontrols to string on c#?

So I have tried GetType() but for some reason, It does include the namespace... Does C# not have a property for a class specifying its name? For example: public class Parent : System.Web.UI.UserControl { public someFunction(){ Child child = new Child(); Console.WriteLine(child.ThePropertyThatContainsTheName); } }...

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 abstract class type? Is it even possibl...

EntLib Validation problem on GetType(object) - expects string not object???

I have an Address object that I am trying to validate data against using EntLib: Given the following method: <ValidatorComposition(CompositionType.And, Ruleset:="FraudAnalysis")> _ <NotNullValidator(MessageTemplate:="Billing address is required.", Ruleset:="FraudAnalysis")> _ <TypeConversionValidator(GetType(Address), MessageTemplate:=...

How do I convert a type to a string that can be compiled in vb .net?

I need a function that will convert a type to a string, for example: Dim foo as Dictionary(of Dictionary(of Integer, String), Integer) Debug.WriteLine(TypeToString(GetType(foo))) In the debug output, I'd expect to see something equivalent to: Dictionary(of Dictionary(of Integer, String), Integer) ...

How to get the type of the class for comparison

I have this object which is an instance of a superclass. I want to know which subclass that object really is, so that I can decide what to do with it. There is this getClass() method but it's apparently not used for comparison issues. How can I get the sub-type of my object? ...

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 to figure out that ClassA "owns" i....

How to dynamically use the PropertyType reflection attribute to create a respective typed Function<>?

I want to use type returned by PropertyType to create a typed function. I found this similiar http://stackoverflow.com/questions/914578/using-type-returned-by-type-gettype-in-c but this mentions how to create a list but does not mention how we can create a Func<>. Please help me out. Pseudocode: PropertyInfo inf = typeof(SomeClass).Get...

C# class type - How to determine whether it is a standard .net framework class

C# / .net framework What is the most reliable way to determine whether a class (type) is a class provided by the .net framework and not any of my classes or 3rd party library classes. I have tested some approaches The namespace, e.g. starting with "System." The Codebase of the assembly, where the dll is located All this "feels" a...

How to hide GetType() method from COM?

I made an automation Add-In for Excel, and I made several functions (formulas). I have a class which header looks like this (it is COM visible): [ClassInterface(ClassInterfaceType.AutoDual)] [ComVisible(true)] public class Functions {} In a list of methods, I see: ToString(), Equals(), GetHashCode() and GetType() methods. Since ...