Op Englesch liesen Editéieren

Deelen iwwer


PropertyInfo.GetGetMethod Method

Definition

Returns a MethodInfo representing the get accessor for this property.

Overloads

GetGetMethod(Boolean)

When overridden in a derived class, returns the public or non-public get accessor for this property.

GetGetMethod()

Returns the public get accessor for this property.

GetGetMethod(Boolean)

Source:
PropertyInfo.cs
Source:
PropertyInfo.cs
Source:
PropertyInfo.cs

When overridden in a derived class, returns the public or non-public get accessor for this property.

C#
public abstract System.Reflection.MethodInfo? GetGetMethod(bool nonPublic);
C#
public abstract System.Reflection.MethodInfo GetGetMethod(bool nonPublic);

Parameters

nonPublic
Boolean

Indicates whether a non-public get accessor should be returned. true if a non-public accessor is to be returned; otherwise, false.

Returns

A MethodInfo object representing the get accessor for this property, if nonPublic is true. Returns null if nonPublic is false and the get accessor is non-public, or if nonPublic is true but no get accessors exist.

Implements

Exceptions

The requested method is non-public and the caller does not have ReflectionPermission to reflect on this non-public method.

Examples

The following example displays the public or non-public get accessor for the specified property.

C#
using System;
using System.Reflection;

// Define a property.
public class Myproperty
{
    private string caption = "A Default caption";
    public string Caption
    {
        get{return caption;}
        set {if(caption!=value) {caption = value;}
        }
    }
}

class Mypropertyinfo
{
    public static int Main()
    {
        Console.WriteLine ("\nReflection.PropertyInfo");

        // Get the type and PropertyInfo for two separate properties.
        Type MyTypea = Type.GetType("Myproperty");
        PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption");
        Type MyTypeb = Type.GetType("System.Reflection.MethodInfo");
        PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("MemberType");

        // Get and display the GetGetMethod method for each property.
        MethodInfo Mygetmethodinfoa = Mypropertyinfoa.GetGetMethod();
        Console.Write ("\nGetAccessor for " + Mypropertyinfoa.Name
            + " returns a " + Mygetmethodinfoa.ReturnType);
        MethodInfo Mygetmethodinfob = Mypropertyinfob.GetGetMethod();
        Console.Write ("\nGetAccessor for " + Mypropertyinfob.Name
            + " returns a " + Mygetmethodinfob.ReturnType);

        // Display the GetGetMethod without using the MethodInfo.
        Console.Write ("\n" + MyTypea.FullName + "." + Mypropertyinfoa.Name
            + " GetGetMethod - " + Mypropertyinfoa.GetGetMethod());
        Console.Write ("\n" + MyTypeb.FullName + "." + Mypropertyinfob.Name
            + " GetGetMethod - " + Mypropertyinfob.GetGetMethod());
        return 0;
    }
}

Remarks

This property is the MethodInfo representing the get accessor.

To use the GetGetMethod method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetGetMethod method.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.5, 1.6, 2.0, 2.1

GetGetMethod()

Source:
PropertyInfo.cs
Source:
PropertyInfo.cs
Source:
PropertyInfo.cs

Returns the public get accessor for this property.

C#
public System.Reflection.MethodInfo? GetGetMethod();
C#
public System.Reflection.MethodInfo GetGetMethod();

Returns

A MethodInfo object representing the public get accessor for this property, or null if the get accessor is non-public or does not exist.

Implements

Remarks

This is a convenience method that provides an implementation for the abstract GetGetMethod method with the nonPublic parameter set to false.

To use the GetGetMethod method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetGetMethod method.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.5, 1.6, 2.0, 2.1