When do I need to override constructors for GetConstructor?
-
I have a class class1, which has a constructor and one method:
public class1(TextReader tr)
{
InitFromTextReader(tr);
}
protected virtual bool InitFromTextReader(TextReader tr)
{
// init code goes here
}It has subclass
class1_1
, which also has a subclassclass1_1_1
. These two classes both overrideInitFromTextReader
. The text stream I am reading has class names embedded in it, just before the data for the object. So I read the class name, get theType
object for it and ask for the constructor that takes a TextReader as it's one and only parameter.GetConstructor
is not finding that constructor for the subclasses. I usedGetConstructors
, and it just does not exist. So, all that to ask: Even though they do not do a dang thing, do I need to write constructorspublic class1_1(TextReader tr)
: base(tr)
{
}and
public class1_1_1(TextReader tr)
: base(tr)
{
}so that I can find them with
GetConstructor
? It this kind of thing wot causes unrest!Learn to write self marginalizing code! Call 1-888-BAD-CODE
-
I have a class class1, which has a constructor and one method:
public class1(TextReader tr)
{
InitFromTextReader(tr);
}
protected virtual bool InitFromTextReader(TextReader tr)
{
// init code goes here
}It has subclass
class1_1
, which also has a subclassclass1_1_1
. These two classes both overrideInitFromTextReader
. The text stream I am reading has class names embedded in it, just before the data for the object. So I read the class name, get theType
object for it and ask for the constructor that takes a TextReader as it's one and only parameter.GetConstructor
is not finding that constructor for the subclasses. I usedGetConstructors
, and it just does not exist. So, all that to ask: Even though they do not do a dang thing, do I need to write constructorspublic class1_1(TextReader tr)
: base(tr)
{
}and
public class1_1_1(TextReader tr)
: base(tr)
{
}so that I can find them with
GetConstructor
? It this kind of thing wot causes unrest!Learn to write self marginalizing code! Call 1-888-BAD-CODE
Yes you would need to implement the constructors in your derived classes. The base constructor only constructs the base object. Really you don't need the InitFromTextReader because you should be able to perform the initialization in the constructors (which pass the TextReader to it's base class).
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
-
Yes you would need to implement the constructors in your derived classes. The base constructor only constructs the base object. Really you don't need the InitFromTextReader because you should be able to perform the initialization in the constructors (which pass the TextReader to it's base class).
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
Thanks, I appreciate the quick response, but I was afraid of that. I needed the
InitFromTextReader
anyway, as I do not just use it from the constructor. So what it comes down to is you can not implicitly inherit a constructor's capabilities in c#: To access the capability, you must override it to explicitly expose it, even if there is no other functionality added.Learn to write self marginalizing code! Call 1-888-BAD-CODE
-
Thanks, I appreciate the quick response, but I was afraid of that. I needed the
InitFromTextReader
anyway, as I do not just use it from the constructor. So what it comes down to is you can not implicitly inherit a constructor's capabilities in c#: To access the capability, you must override it to explicitly expose it, even if there is no other functionality added.Learn to write self marginalizing code! Call 1-888-BAD-CODE
Correct.
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com