-3

Say we have

using (var lobo = new MyGymEntities())
{
}

I would like to get the DbContext name (in this case is MyGymEntities) with reflection, is this possible?

I would like to store the string value into a variable..

Edit:

I do not want to type MyGymEntities.GetType().Name/FullName to do this.

I'm asking if it is possible to somehow obtain this information from the assembly such as

Assembly.GetExecutingAssembly().GetName().Name; //Return Projectname

UPDATE

The following solved my problem, in case anyone

var a = Assembly.GetExecutingAssembly()
                .DefinedTypes
                .Where(x => x.BaseType?.Name == "DbContext").ToList();

Console.WriteLine(a.First().FullName);

Ok, guys, I'll explain why this question is not a duplicate and why I want to get the name of the DbContext.

I'm well aware that the name property of an object can be accessed by calling the GetType().Name on it, but this method can only be called by an object itself meaning I need to specify which objects name attribute I would like to retrieve.

Ex:

MyObject.GetType().Name;

However what I want to do is: to get the DbContextName(if there's any) that is on the project, solution.

why do I need this? because I have generic Forms that handle my queries

inside my Custom Forms currently, I have something like this

MyGymEntities context = new MyGymEntities();

now If I copy and paste the current solution folder to create a new project I'll have to rename the DbContext shown above to the name of the new DbContext for my solution.

however, If I use this to get the ContextName, than I'll create a new instance of the DbContext of this name and use it on my Custom Forms, therefore will need less configuration for each project I'll be creating...

var ContextName = Assembly.GetExecutingAssembly()
            .DefinedTypes
            .Where(x => x.BaseType?.Name == "DbContext").ToList().First().Name;

Thanks!

Asım Gündüz
  • 1,257
  • 3
  • 17
  • 42
  • 3
    `GetType().Name`? – FakeCaleb Oct 09 '17 at 09:10
  • 1
    Possible duplicate of [C# getting its own class name](https://stackoverflow.com/questions/2113069/c-sharp-getting-its-own-class-name) – FakeCaleb Oct 09 '17 at 09:12
  • no this is not a duplicate project, I'm sorry for the bad english, I have updated my question to be more clear – Asım Gündüz Oct 09 '17 at 09:14
  • What results do you expect when there are multiple `DbContext` definitions in an assembly? Do you want to get all types derived from `DbContext` in an assembly? – bassfader Oct 09 '17 at 09:21
  • well if there are multiple DbContext it's not a problem since I could add a property to the main one to identify the one I want. I just want to get the name to create a new instance of it with reflection, the reason I am doing this is I'm creating the instance of it inside a custom form. I could of course type the dbcontext name manually but this way I could just copy paste the project and immediately be able to start coding without doing any configurations in the code – Asım Gündüz Oct 09 '17 at 09:35
  • 1
    Your answer is far more complicated, and less performant than just calling `GetType().Name`. Sounds like you have a different problem that you're not telling us about. One thing you may be missing out on is that you don't have to call that on `MyGymEntities`, but on `lobo`. – krillgar Oct 09 '17 at 10:18
  • There is nothing I'm not telling, I want to get the Context name on the project to be able to create a new instance of it at runtime and so far the answer I've provided is the only thing I have found. – Asım Gündüz Oct 09 '17 at 11:06
  • Hard to imagine that `.First()` would match any sensible business logic. Do you always get your types from a lottery? – H H Oct 09 '17 at 12:44
  • It sounds to me like you are re-inventing Dependency-Injection. Why not use one of the existing, well-tested, libraries that already do this, like [Unity](https://msdn.microsoft.com/en-us/library/dn223671(v=pandp.30).aspx)? – Bradley Uffner Oct 09 '17 at 12:52

1 Answers1

1

BaseType only returns the immediate base type, and will break if you later introduce a custom base DbContext. You can get all the DbContext types in an assembly with something like:

var dbContextTypes = Assembly.GetExecutingAssembly()
                             .DefinedTypes.Where(t => typeof(DbContext).IsAssignableFrom(t))
                             .ToList();
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67