I'm attempting to dynamically register entities and configurations with a context (ef4 code-only). I would normally do:
Private Shared Sub ConfigureDatabase(ByRef Builder As ContextBuilder(Of ContextExtension))
'Load configurations for each of the tables (Entity sets) in our database...
ConfigureEntity(Builder, New ContactConfig)
End Sub
Private Shared Sub ConfigureEntity(Of T)(ByRef Builder As ContextBuilder(Of ContextExtension), ByVal config As EntityConfiguration(Of T), ByVal setName As String)
'Register the entity configuration with the builder
Builder.Configurations.Add(config)
'Register the entity set name with the builder
Builder.RegisterSet(Of T)(setName)
End Sub
Where ContactConfig is a class which inherits EntityConfiguration(Of Contact) and Contact is a class which implements an interface IEntity (IEntity is common to all entities). So... I need to search a namespace (say Project.DB.Client) for all signatures that match:
EntityConfiguration(Of <Class which implements IEntity>)
How can I achieve this?
I'm working on a new DAL for my latest project. As the performance is essential I'm avoiding late binding/reflection as much as possible. One central part is the DAL function for populating Business Objects.One class is called in order to connect to the database with the given SQL (stored procedures can't be used in this environment) and the given Business Object or List is populated. As different Business Object types will be populated by the same function I made an Interface that all of the Business Objects implements with the Sub called "Fill". The question is - will reflection be used in this scenario? My thought was as I'm "describing" the objects trought the interface the compiler doesn't need to use Reflection.
Public MustInherit Class Column Public ReadOnly Property ReturnSomethingUseful() As Object Get 'return something useful
[code]....
But this gives the following error:
Public Overrides Function ParseValue(sValue As String) As Boolean' cannot override 'Public Overridable Function ParseValue(sValue As String) As Object' because they differ by their return types.
I accept that you can't do this, but I'd like to be able to preserve the semantics of what I'm. trying to do, which is to have an untyped version that deals with Object, but a typed version in derived classes that knows about the specific type T.
I'm using EF 4.3.1 in VS 2010 (.Net 4.0) to load a number of reference tables from a database in order to bind them to controls in a WinForms app.At form load, I'm pre-fetching the data so that it's stored locally, and I'm creating a dictionary of BindingSource objects that I can use to bind the local data to the controls.The intent is to minimize the impact of the EF's self validation on a cold query in order to load forms faster and improve UI responsiveness.I've written code to create and fetch the BindingSource object for a given DbSet collection as follows:
Private _dictBindings As New Dictionary(Of String, BindingSource) Private Sub ValidateBinding(Of T As Class)(ByRef DbCollection As DbSet(Of T)) Dim strClassName As String = DbCollection.[GetType]().GetGenericArguments(0).Name If Not _dictBindings.ContainsKey(strClassName) Then[code].....
However, I'd like to call ValidateBinding on all DbSet collections in the Model on startup, and I'd like to use reflection to iterate through the available collections in the context because we're currently loading 66 tables and could add more later.I've written the following code:
For Each propSet As PropertyInfo In Db.GetType.GetProperties(BindingFlags.Instance Or BindingFlags.Public).Where(Function(P) P.PropertyType.IsGenericType) ValidateBinding(propSet.GetValue(Db, Nothing)) Next
but it won't work as propSet.GetValue() returns an Object instead of DbSet(of T).I also can't cast the object to a DbSet of the appropriate type, even though I know the type through reflection.I don't have access to the C# Dynamic type,and I know it's a pain to mix generics with reflection, but is there a solution for which I can pass in a reflected DbSet to my functions?Perhaps something using Method.Invoke?
I am trying to create a list of a generic type in vb.net 2.0 framework. This is the generic type definition:
Public Class GenericParamMap(Of T) Public Sub New(ByVal pParamName As String, ByVal pPropValue As T) mParamName = pParamName
[Code]....
The compiler does not allow a "T" in the method's parameter because it's not defined, but I'm not sure how or where to define it. I thought it was okay to have a generic method definition.
I am trying to write a generic method, to avoid code duplication, which will create or activate a Form as an MDI children, based on its type. But I have to lines in error (see comments).
I'm compiling a VB.Net 2.0 app (created in VS2008) using msbuild, and now I've added a generic return type, it's giving me the following:
Warning: Type library exporter encountered a generic type instance in a signature. Generic code may not be exported to COM.
Having just spent ages removing all of the previous warnings, I don't really want to add a new one. Any idea how to get rid of it (aside from not using generics)?I don't know what details I'd put in the attribute, or what number to put in the project-level ignore list.
I have a generic Class I'm using to hold information loaded from a database.I have a method which takes a DataRow as an argument, uses the object's known column name and extracts the data from the DataRow, such that:Dim loadData As T = CType(myDataRow("myColumnName"), T))works as my default assignment in most cases.Unfortunately, due to some horrifying design constraints, some of my columns may be null, and may also be taken from enumerations.This means that when <T> is Nullable(Of SomeEnumeration) the above code does not work because I can't cast 0 directly to SomeEnumeration.Zero.Is there some way to check whether <T> is Nullable(Of [Enum])? Or some way to write a method which allows Integers to be cast to Nullable(Of [Enum])?I feel like I'm forgetting something that would allow me to write one of the other of these, but my weak google-fu is turning up nothing.
EDIT: Okay, thanks to dasblinkenlight's answer below, I can detect when this circumstance is occurring, but what I need to do now is to take a type <T> which I know is Nullable(Of SomeClass), get a type reference to SomeClass and then create a new object of type Nullable(Of SomeClass) and assign that to LoadData.My problem was that I had a lot of difficulty in finding any function which would accept baseType as an actual Type.Parse accepted baseType as a parameter, I knew baseType was an [Enum] type because of dasblinkenlight's code, so I was, in this instance, able to code a solution. It's a solution which is very specific to my problem (i.e., T is Nullable(of SomeEnumeration)), but it's a solution nonetheless.
I'm looping all the properties in an object via reflection:
For Each p As PropertyInfo In values.[GetType]().GetProperties() If p.CanRead Then 'Do stuff End If Next
how to determine whether the property in question is a generic List(Of T)? If it is I need to loop the list itself.
I've experimented with GetType and TypeOf but have not managed to get anything working.
To clarify, I want to keep this generic. I do not want to specify the type of T, I need to loop the list items and call the ToString method on each item. T could be one of a number of different types (application specific reference types). Is it possible to do this without specifying types?
In trying to add a bit of usage variety to a generic class I'm working on, I ran into this issue with trying to cast an object into an interface instance where the interface is defined inside the generic class.
1) VB Allows non-type template parameters2) VB supports explicit specialization 3) VB allows the type parameters to be used as the base class for the generic type4) VB allows a generic type parameter itself to to be a generic 5) VB enforces that all codes are valid for all types of parametrs
What I wanted to do was, given the table name (as string), use reflection to instantiate the get method for specific fields (defined as properties with Get and Set methods.
I am trying to write a function that has an object parameter. That object will always be an BindingList. That BindingList will be of some unknown (at design Time) class. I think I've figured out how to get the type of the collection object, but now here the tricky part. I'm trying to create a function that can handle any type of collection and be able to return an item from that collection. I need to create a new object of that type and return it from the function. [Code]
CREATE TABLE [LogLevel] ( [Id] int primary key ,[Name] nvarchar(50) not null
[code]....
After creating a fresh endity model, I add the two tables above. When I try to build I get the following errors...
Type argument 'Inxsol.CommandPlan.Data.Model.Log.LogLevel' does not satisfy the 'Class' constraint for type parameter 'TEntity'. Value of type 'System.Data.Objects.DataClasses.EntityReference(Of Inxsol.CommandPlan.Data.Model.LogLevel)' cannot be converted to 'System.Data.Objects.DataClasses.EntityReference(Of Inxsol.CommandPlan.Data.Model.Log.LogLevel)'.
I am trying to create an Excel file using reflection. The reason, the application will be running on many machines some of which may or not have excel installed. I decided to embed the "Microsoft.Office.Interop.Excel.dll" and via reflection generated the excel spreadsheet. The code I am trying to resemble is:
Is there a way for a property to access its own name and type at runtime using reflection? I want to access this info without hard coding the name or index of the property in the class.
Simple Example Code:
Private ReadOnly Property MyProperyName() As String Get Console.WriteLine((Get Current Property Info).Type.ToString) Console.WriteLine((Get Current Property Info).Name)
I've got a set of static "enumeration" classes that I'm using to hold meaningful variable names to represent meaningless code values I receive on an input file. Here's an example of one.
Public Class ReasonCodeValue Private Sub New() End Sub
Public Function f(ByVal t As System.Type) As Object Return t.GetConstructor(New System.Type() {}).Invoke(New Object() {}) End Function
I need to pass values to the Constructor as
Public Function f(ByVal t As System.Type) As Object Return t.GetConstructor(New System.Type() {someInteger,someString,etc.etc}).Invoke(New Object() {}) End Function
Also I have 3 classes of Type T, with all having different parametric constructor. It's important for me to have it generic as the Classes of type T might increase in future with less or more parameters.
I'm trying to create an object Of a specific type. I've got the following code, but it fails because it can't cast the new table object to the one that is already defined. I need table to start of an IEnumerable type so I can't declare is an object.
Public sub getTable(ByVal t as Type) Dim table As Table(Of Object) Dim tableType As Type = GetType(Table(Of )).MakeGenericType(t)
[code]....
is there a way of changing a variable type at runtime?
I am in VB.net and wondering if there is a way to call a search from a console application.For example when the user clicks cancel on the Input Box it brings up the file browser for their computer allowing them to choose a place to save the file.[code]
I'm trying to check if a variable has been defined as a nullable Guid. eg.
Dim myGuid As Nullable(Of Guid) or Dim myGuid As Guid?
It seems doing a myGuid.GetType returns the underlying type, that is type Guid, not Guid?. So testing myGuid.GetType Is GetType(Guid?) always returns False.How do I find out if myGuid is a nullable type?
Ed: I can do the following, which correctly returns True for "Guid?" and False for "Guid":
Not Nullable.GetUnderlyingType(GetType(Guid?)) Is Nothing
The problem is that I don't know how to retrieve the nullable type from the variable itself, in order to test it. I've only been able to get the underlying (non-nullable) system type.
I've written a db helper function. I pass it an object comprised of public members, representing the row data of a table. The members are the table columns.Using reflection, I loop through these public members to create an INSERT statement for a command object and populate its parameters with the values in those members. So far so good.But now there's a table which has a uniqueidentifier column which I must not populate from the row object, as it defaults to "NEWID()" (using SQL Server 2008). Instead of skipping all Guid columns, which would be easy, I only want to skip ones defined in the row data class as "Guid" (non-nullable).Basically, I'm using the Guid? (Nullable) type to indicate it's ok to populate that uniqueidentifier column with data. If it's non-nullable, that tells me to skip it because the column has a NEWID() default value.
I'm passing a type name and some parameters from C# code into a navigation framework written in VB. The navigation framework looks for a constructor on the type that matches the parameters passed in using Type.GetConstructor(Types()). The constructor that I'm looking for expects an array of integers Integer() in vb. But it gets an array of System.Int32. I've gone so far as to try this:
[Code]...
And the VB code still sees System.Int32 on the other end, which means that it doesn't find the constructor.