How to get a variable's System Data Type.
I need to find all the variables that are of type, System.IO.StreamReader, using fxcop. How do i achieve this.
If not possible do let me know the reason.
It would be helpful if some sample code would be available.
regards
venkateshaperumal

How to get a variable's System Data Type.
basittanveer
Hi Jeffrey
Thanks for the response it was helpful
regards
venkatesaperumal
marrow_Jamaica
Hi Venkatesaperumal
Depends what variable you're talking about. For fields on classes, you can do the following:
TypeNode m_streamReader;
public override void BeforeAnalysis()
{
m_streamReader = FrameworkAssemblies.Mscorlib.GetType(Identifier.For("System.IO", Identifier.For("StreamReader"));
}
public ProblemCollection override Check(Member member)
{
Field field = member as Field;
if (field == null) return null;
if (field.Type.IsAssignableTo(m_streamReader))
{
//do rest of your analysis
}
}
If you're looking for local variables (inside the method) modify the code slightly:
TypeNode m_streamReader;
public override void BeforeAnalysis()
{
m_streamReader = FrameworkAssemblies.Mscorlib.GetType(Identifier.For("System.IO", Identifier.For("StreamReader"));
}
public ProblemCollection override Check(Member member)
{
Method method = member as Method;
if (method == null) return null;
LocalList locals = Method.Instructions[0] as LocalList;
if (locals == null) return null;
foreach(Local local in locals)
{
if (loacl.Type.IsAssignableTo(m_streamReader))
{
//do rest of your analysis
}
}
}
Regards,
Jeffrey