|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Is It Possible to Retrieve a List of Declared Variables?Public Class MyClass Public Prop1 as Integer Public Prop2 As Integer Public Prop3 As Integer End Class Is it possible to retrieve a list of the variables or objects declared within an instance of that class if they are declared with Public (or Friend) scope? e.g. Public Class SomeOtherClass Public Sub Experiment Dim obj As MyClass = New MyClass ' Get list of variables declared within obj ' ' List the variable names: ' "Prop1" ' "Prop2" ' "Prop3" End Sub End Class Or can something like this only be done with Properties and Methods? - Don "Don" <unkn***@oblivion.com> schrieb: 'GetType(MyClass1).GetFields(...)'.> Public Class MyClass > Public Prop1 as Integer > Public Prop2 As Integer > Public Prop3 As Integer > End Class > > Is it possible to retrieve a list of the variables or objects declared > within an instance of that class if they are declared with Public (or > Friend) scope? -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message This worked for Publicly declared variables (which is great! thanks!), but news:OCjWRA4IGHA.1188@TK2MSFTNGP14.phx.gbl... > > 'GetType(MyClass1).GetFields(...)'. is there a way to get it to work with variables of scope Friend as well? - Don "Don" <unkn***@oblivion.com> schrieb: Take a look at the overloaded versions of 'GetFields' which accept a >> 'GetType(MyClass1).GetFields(...)'. > > This worked for Publicly declared variables (which is great! thanks!), > but is there a way to get it to work with variables of scope Friend as > well? 'BindingFlags' parameter. Check out 'BindingFlags.NonPublic'. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> I figured it had something to do with that, but I couldn't find any obvious
argument or combination of arguments that works. In my test case, I have a class (a Form, actually) with one Public variable, one Friend, and one Private. Within the form's Load event I call Me.GetType.GetFields(). If I call GetFields with no arguments, then the fieldinfo for the Public variable is returned. If I call GetField with BindingFlags.NonPublic, I get nothing. And if I call GetField with BindingFlags.Public, I still get nothing. I don't know if I'm missing some other BindingFlag or not. It didn't behave as I expected it to, given what I could deduce from the flag name and the online help. Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message news:OZf4k$4IGHA.676@TK2MSFTNGP10.phx.gbl... > "Don" <unkn***@oblivion.com> schrieb: >>> 'GetType(MyClass1).GetFields(...)'. >> >> This worked for Publicly declared variables (which is great! thanks!), >> but is there a way to get it to work with variables of scope Friend as >> well? > > Take a look at the overloaded versions of 'GetFields' which accept a > 'BindingFlags' parameter. Check out 'BindingFlags.NonPublic'. > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/> "Don" <unkn***@oblivion.com> schrieb: Include 'BindingFlags.Instance' ('BindingFlags.Instance Or > In my test case, I have a class (a Form, actually) with one Public > variable, one Friend, and one Private. Within the form's Load event I > call Me.GetType.GetFields(). > > If I call GetFields with no arguments, then the fieldinfo for the Public > variable is returned. If I call GetField with BindingFlags.NonPublic, I > get nothing. And if I call GetField with BindingFlags.Public, I still get > nothing. I don't know if I'm missing some other BindingFlag or not. It > didn't behave as I expected it to, given what I could deduce from the flag > name and the online help BindingFlags.Public', for example). -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Don,
Did you review the URL I gave you? http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtypeclassgetfieldstopic2.asp As Herfried points out, you need both BindingFlags.Instance to say instance fields (or BindingFlags.Static for Shared fields) plus you need BindingFlags.Public to include public fields... There are other BindingFlags available to include or exclude other fields (such as inherited fields). So be certain to review the above page. -- Hope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Don" <unkn***@oblivion.com> wrote in message news:RdxCf.233435$tl.52321@pd7tw3no...Show quoteHide quote |I figured it had something to do with that, but I couldn't find any obvious | argument or combination of arguments that works. | | In my test case, I have a class (a Form, actually) with one Public variable, | one Friend, and one Private. Within the form's Load event I call | Me.GetType.GetFields(). | | If I call GetFields with no arguments, then the fieldinfo for the Public | variable is returned. If I call GetField with BindingFlags.NonPublic, I get | nothing. And if I call GetField with BindingFlags.Public, I still get | nothing. I don't know if I'm missing some other BindingFlag or not. It | didn't behave as I expected it to, given what I could deduce from the flag | name and the online help. | | | "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message | news:OZf4k$4IGHA.676@TK2MSFTNGP10.phx.gbl... | > "Don" <unkn***@oblivion.com> schrieb: | >>> 'GetType(MyClass1).GetFields(...)'. | >> | >> This worked for Publicly declared variables (which is great! thanks!), | >> but is there a way to get it to work with variables of scope Friend as | >> well? | > | > Take a look at the overloaded versions of 'GetFields' which accept a | > 'BindingFlags' parameter. Check out 'BindingFlags.NonPublic'. | > | > -- | > M S Herfried K. Wagner | > M V P <URL:http://dotnet.mvps.org/> | > V B <URL:http://classicvb.org/petition/> | | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in Sorry, no. I read Herfried's response first and went with that, not message news:%231pAre8IGHA.596@TK2MSFTNGP10.phx.gbl... > Don, > Did you review the URL I gave you? noticing your post later on. I was poring over the BindingFlags Enumeration help, but I find the documentation provided with VS.NET to be typically pretty obscurely written with poor code examples (at least compared with pre-.NET Visual Studio documentation). - Don Don,
Use the overloaded GetFields, & specify the option that indicates Friend. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtypeclassgetfieldstopic2.asp -- Hope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Don" <unkn***@oblivion.com> wrote in message news:mxwCf.233143$tl.88327@pd7tw3no...Show quoteHide quote | | "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message | news:OCjWRA4IGHA.1188@TK2MSFTNGP14.phx.gbl... | | > | > 'GetType(MyClass1).GetFields(...)'. | | This worked for Publicly declared variables (which is great! thanks!), but | is there a way to get it to work with variables of scope Friend as well? | | - Don | | Don,
You can using Reflection. You need to start with a System.Type object. You can use the GetType keyword to get a type object of known class, or you can use Object.GetType to get a type object of a known object. Once you have a Type object, you can call Type.GetFields & Type.GetProperties to get fields & properties of the respective type. Watch the overloads, as you can "fine tune" what you are looking for. Dim aType As Type = GetType([MyClass]) For Each fi As System.Reflection.FieldInfo In aType.GetFields() Debug.WriteLine(fi.Name, "field") Next For Each pi As System.Reflection.PropertyInfo In aType.GetProperties() Debug.WriteLine(pi.Name, "property") Next For Each mi As System.Reflection.MethodInfo In aType.GetMethods() Debug.WriteLine(mi.Name, "method") Next Look for other methods on System.Type to retrieve other information on that type. -- Hope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Don" <unkn***@oblivion.com> wrote in message news:EevCf.457280$ki.60104@pd7tw2no...Show quoteHide quote | Say I have a class like so: | | Public Class MyClass | Public Prop1 as Integer | Public Prop2 As Integer | Public Prop3 As Integer | End Class | | Is it possible to retrieve a list of the variables or objects declared | within an instance of that class if they are declared with Public (or | Friend) scope? | | e.g. | | Public Class SomeOtherClass | | Public Sub Experiment | | Dim obj As MyClass = New MyClass | | ' Get list of variables declared within obj | ' | ' List the variable names: | ' "Prop1" | ' "Prop2" | ' "Prop3" | | End Sub | | End Class | | Or can something like this only be done with Properties and Methods? | | - Don | |
Whats's the problem using a enum like this?
Converted a solution to VS2005 and it is complaining: Variable 'PF2' is used before it has been assi Detect Concurrent Running Instances Problem with installer made in VS 2003.NET setting datagrid column widths in code? Inheritance and function issue FileDownload with WebBrowser Control - disable the Save? Run from Server or Workstation Force ASP.NET shadow directory to update? |
|||||||||||||||||||||||