Code Dom

  • Thread starter Thread starter Wayne
  • Start date Start date
W

Wayne

I have an instance of a System.CodeDom.CodeNamespace. I am currently
spinning through the types, and the members of each type. I am trying to
find the Codememberfields that require creation before access. I then want
to add to the constructor the creation of each of these Items.

I have been able to figure out how to add the constructor to a type if it
doesn't have one. However, I am stuck with how to figure out if the current
member requires creation, and how to modify the code dom to add said
creation to the constructor.

Any help would be appreciated.

Thanks
Wayne
 
Hi Wayne,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know how to check if a type
already have a constructor. If there is any misunderstanding, please feel
free to let me know.

Based on my research, I didn't find any method or properties to know if a
type has already had a constructor. I think we can iterate throught the
CodeTypeDeclaration.Member collection to check each member. If one member
is a CodeConstructor object it means that the object has a constructor.
Here is an example:

private bool HasConstructor(CodeTypeDeclaration t)
{
foreach(CodeTypeMember m in t.Members)
{
if(m is CodeConstructor)
return true;
}
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
No I have figured out how to get the constructor, I did basically what you
have below. what I need is the following:

1) how to tell if a private member is required to be created, meaning it's
not a string, int, datetime, etc...
2) once I determine that it does need to be created how to insert the code
into the dom, so that the member is created and initialized in the
constructor

Thanks
Wayne
 
Hi Wayne,

We can go throught each member to check CodeMemberField.Type property to
see if this field needs initialization. And use
CodeMemberField.InitExpression to create the initialization expression. For
more information, please check the following link.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemcodedomcodememberfieldclassinitexpressiontopic.asp

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Part of that is where I'm lost, I've not quite got the syntax correct yet to
check the type. Can you help with that? also if I understand correctly then
I will have to do

if type is not INt and type is not string and type is not etc....

Correct?

Thanks
Wayne
 
Hi Wayne,

CodeMemberField.Type will return a CodeTypeReference object. We will get
the type name with CodeTypeReference.BaseType property. I think we have to
check for each type to see if it needs initialization.

You can also use Type.GetType method to create a type object and use
IsClass or IsPrimitive property to see if this type needs initialization.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
This worked great but I've run into a small issue.

I have an object where the properties Set method does something to the new
value. So I directly init the code member field. How would one go about
creating an instance in the constructor and set the new instance to the
Property?

Thanks
Wayne

--
Thanks
Wayne Sepega
Jacksonville, Fl

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
Never mind, I actually did a bit more looking in the help (very large help
file that the MSDN is) and found what I needed.

--
Thanks
Wayne Sepega
Jacksonville, Fl

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
Back
Top