Overwriting of Property-Attributes in a Derived Class

H

Hans Bampel

Hello group,

i want to overwrite or manipulate a attribute of a property in a
derived class. I use a attribute DBInfo via reflection on my
properties to set the parameters in a SQL-statement dynamically. Now
in a derived class (similar table in the DB), the name of the column a
other one.

what i have

class DBInfoAttribute : Attribute
{
public SqlDbType sqlDbType;
public int length;
public string name;

public DBInfoAttribute(string name, SqlDbType sqlDbType, int length)
{
this.name = name;
this.sqlDbType = sqlDbType;
this.length = length;
}
}

class a
{
protected string name;
[DBInfo("Name", SqlDbType.Varchar, 50)]
public string Name
{
..
}

//something like that
protected void ChangeDbInfoAttribute(string sPropertyName, string
newName,
SqlDbType newSqlDbType, int newLength)
{
PropertyInfo[] piA = this.GetType().GetProperties();
foreach (PropertyInfo pi in piA)
{
if (pi.Name == sPropertyName)
{
DBInfoAttribute dbia = GetDBTypeAttribute(pi);
if (dbia != null)
{
dbia.Name = newName;
dbia.sqlDbType = newSqlDbType;
dbia.length = length;
}
}
}
}

class b : a
{
..//maybe im constructor
ChangeDbInfoAttribute("Name", "Surname", SqlDbType.Varchar, 35)
..
//a call of ChangeDbInfoAttribute is possible, the debugger shows
//the new values till i come out of the scope of
ChangeDbInfoAttribute
}

My actual way is to new-introduce the property Name with other
Attribute-Values.
is it possible to go a way like above?

Thank you & Greetings,
Hans
 
P

Patrick Steele [MVP]

web- said:
i want to overwrite or manipulate a attribute of a property in a
derived class.

You can't. Attributes are placed in the metadata and used by the
runtime. Sure you can *look* at the attributes via code, but you can't
change them.
 

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