In C#, get the name of a class as a string.

c# entity-framework

Question

Is it possible to change a class name in C# into a string?

The is a component of the Entity Framework. When conducting a query, the Include method accepts a dot-delimited list of strings to join on. I have the class model for the thing I want to join, and I want to be able to reference it safely at compile time for refactoring and future code maintenance.

So, is there a method I could do the following:

class Foo
{
}

tblBar.Include ( Foo.GetType().ToString() );

Without an object, I don't believe I can execute GetType(). Any thoughts?

1
65
10/7/2008 6:14:46 PM

Accepted Answer

It's not a class name, Include needs the name of a property.. As a result, you need the name of the desired property, not the name of its type. You may achieve it through reflecting..

12
10/7/2008 6:21:19 PM

Popular Answer

The usage of.GetType() without giving an example sinceGetType is a strategy.

However, you may do it by determining the name from the type:

typeof(Foo).Name

Chris also said that you may use the assembly qualified name if you need to.

typeof(Foo).AssemblyQualifiedName


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow