CustomAttributeBuilder without ctor

E

EDBrian

I am attempting to dynmically create a class that looks like this:
--------------------------------------
[Table(Name="ClientTable")]
public class ClientTable{
[Column(DbType = "VarChar(400) NOT NULL")]
public string FirstName { get; set; }
}
--------------------------------------

The problem that I am running into is creating the TableAttribute and
ColumnAttribute. How can I create these using the CustomAttributeBuilder?
Every example I see is similar to this one from MSDN:
http://msdn2.microsoft.com/en-us/library/system.reflection.emit.customattributebuilder.aspx

It creates the attribute and passes in the value to the constructor, because
that is how that attribute is defined. But when I look at the
TableAttribute it looks like this:
--------------------------------------
namespace System.Data.Linq.Mapping {
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited =
false)]
public sealed class TableAttribute : Attribute {
public TableAttribute();
public string Name { get; set; }
}
}
 
M

Marc Gravell

I think you need the following overload:

http://msdn2.microsoft.com/en-us/library/kb87eks2.aspx

At a guess (completely untested) - something like (for
TableAttribute):

PropertyInfo[] props =
{ typeof(TableAttribute).GetProperty("Name") };
object[] values = { "ClientTable" };
ConstructorInfo ctor =
typeof(TableAttribute).GetConstructor(Type.EmptyTypes); // default
ctor

CustomAttributeBuilder cab = new CustomAttributeBuilder(
ctor, null, props, values);

Marc
 
E

EDBrian

That worked....thank you.

Now I'm trying to replace my current ClientTable type with the one I
dynamically created.
Is it possible to some how destroy the ClientTable and register my
dynmically created one?
 
N

Nicholas Paldino [.NET/C# MVP]

No, there isn't. The type will be loaded as long as the app domain is
loaded.

You can either derive from the existing ClientTable type to add the
properties you want, or, create a whole new type which has the complete
property set already.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

EDBrian said:
That worked....thank you.

Now I'm trying to replace my current ClientTable type with the one I
dynamically created.
Is it possible to some how destroy the ClientTable and register my
dynmically created one?



Marc Gravell said:
I think you need the following overload:

http://msdn2.microsoft.com/en-us/library/kb87eks2.aspx

At a guess (completely untested) - something like (for
TableAttribute):

PropertyInfo[] props =
{ typeof(TableAttribute).GetProperty("Name") };
object[] values = { "ClientTable" };
ConstructorInfo ctor =
typeof(TableAttribute).GetConstructor(Type.EmptyTypes); // default
ctor

CustomAttributeBuilder cab = new CustomAttributeBuilder(
ctor, null, props, values);

Marc
 
M

Marc Gravell

As per our conversation on the 18th:
You can create new ones, but you can't substitute them for old.

You can (as Nicholas notes) use either inheritance or a common
interface to talk to your new type, but either way it gets quite messy
and typically involves yet more reflection. All of which adds up to
make dynamic types a pain to use...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top