# MemberInfo

***

**1. Retrieving Member Information**

```csharp
// Get the type's member information.
Type type = typeof(MyType);
MemberInfo[] members = type.GetMembers();

// Display the member information.
foreach (MemberInfo member in members)
{
    Console.WriteLine(member.Name);
}
```

**2. Getting the Type of a Member**

```csharp
// Get the type of the member.
MemberInfo member = typeof(MyType).GetMember("MyProperty")[0];
Type memberType = member.MemberType;

// Display the member type.
Console.WriteLine(memberType);
```

**3. Checking if a Member is Public**

```csharp
// Check if the member is public.
MemberInfo member = typeof(MyType).GetMember("MyProperty")[0];
bool isPublic = member.IsPublic;

// Display whether the member is public.
Console.WriteLine(isPublic);
```

**4. Getting the Name of a Member**

```csharp
// Get the name of the member.
MemberInfo member = typeof(MyType).GetMember("MyProperty")[0];
string memberName = member.Name;

// Display the member name.
Console.WriteLine(memberName);
```

**5. Getting the Value of a Field**

```csharp
// Get the value of the field.
FieldInfo field = typeof(MyType).GetField("MyField");
object fieldValue = field.GetValue(myObject);

// Display the field value.
Console.WriteLine(fieldValue);
```

**6. Setting the Value of a Field**

```csharp
// Set the value of the field.
FieldInfo field = typeof(MyType).GetField("MyField");
field.SetValue(myObject, 42);
```

**7. Getting the Value of a Property**

```csharp
// Get the value of the property.
PropertyInfo property = typeof(MyType).GetProperty("MyProperty");
object propertyValue = property.GetValue(myObject);

// Display the property value.
Console.WriteLine(propertyValue);
```

**8. Setting the Value of a Property**

```csharp
// Set the value of the property.
PropertyInfo property = typeof(MyType).GetProperty("MyProperty");
property.SetValue(myObject, 42);
```

**9. Invoking a Method**

```csharp
// Invoke the method.
MethodInfo method = typeof(MyType).GetMethod("MyMethod");
object returnValue = method.Invoke(myObject, new object[] { });

// Display the return value.
Console.WriteLine(returnValue);
```

**10. Getting the Parameters of a Method**

```csharp
// Get the parameters of the method.
MethodInfo method = typeof(MyType).GetMethod("MyMethod");
ParameterInfo[] parameters = method.GetParameters();

// Display the parameters.
foreach (ParameterInfo parameter in parameters)
{
    Console.WriteLine(parameter.Name);
}
```

**11. Getting the Attributes of a Member**

```csharp
// Get the attributes of the member.
MemberInfo member = typeof(MyType).GetMember("MyProperty")[0];
Attribute[] attributes = member.GetCustomAttributes(typeof(MyAttribute), false);

// Display the attributes.
foreach (Attribute attribute in attributes)
{
    Console.WriteLine(attribute.ToString());
}
```

**12. Creating a Custom MemberInfo**

```csharp
// Create a custom MemberInfo.
public class MyMemberInfo : MemberInfo
{
    public MyMemberInfo(string name, MemberTypes memberType)
    {
        this.Name = name;
        this.MemberType = memberType;
    }

    public override string Name { get; }
    public override MemberTypes MemberType { get; }
    public override Type DeclaringType { get; } = typeof(MyType);
}

// Add the custom MemberInfo to the type.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
typeBuilder.AddMember(new MyMemberInfo("MyProperty", MemberTypes.Property));
```

**13. Retrieving Member Information Using Reflection**

```csharp
// Get the type's member information using reflection.
Type type = typeof(MyType);
MemberInfo[] members = type.GetMembers(BindingFlags.Public | BindingFlags.Instance);

// Display the member information.
foreach (MemberInfo member in members)
{
    Console.WriteLine(member.Name);
}
```

**14. Getting the Type of a Member Using Reflection**

```csharp
// Get the type of the member using reflection.
Type type = typeof(MyType);
MemberInfo member = type.GetMember("MyProperty")[0];
Type memberType = member.MemberType;

// Display the member type.
Console.WriteLine(memberType);
```

**15. Checking if a Member is Public Using Reflection**

```csharp
// Check if the member is public using reflection.
Type type = typeof(MyType);
MemberInfo member = type.GetMember("MyProperty")[0];
bool isPublic = member.IsPublic;

// Display whether the member is public.
Console.WriteLine(isPublic);
```

**16. Getting the Name of a Member Using Reflection**

```csharp
// Get the name of the member using reflection.
Type type = typeof(MyType);
MemberInfo member = type.GetMember("MyProperty")[0];
string memberName = member.Name;

// Display the member name.
Console.WriteLine(memberName);
```

**17. Getting the Value of a Field Using Reflection**

```csharp
// Get the value of the field using reflection.
Type type = typeof(MyType);
FieldInfo field = type.GetField("MyField");
object fieldValue = field.GetValue(myObject);

// Display the field value.
Console.WriteLine(fieldValue);
```

**18. Setting the Value of a Field Using Reflection**

```csharp
// Set the value of the field using reflection.
Type type = typeof(MyType);
FieldInfo field = type.GetField("MyField");
field.SetValue(myObject, 42);
```

**19. Getting the Value of a Property Using Reflection**

```csharp
// Get the value of the property using reflection.
Type type = typeof(MyType);
PropertyInfo property = type.GetProperty("MyProperty");
object propertyValue = property.GetValue(myObject);

// Display the property value.
Console.WriteLine(propertyValue);
```

**20. Setting the Value of a Property Using Reflection**

```csharp
// Set the value of the property using reflection.
Type type = typeof(MyType);
PropertyInfo property = type.GetProperty("MyProperty");
property.SetValue(myObject, 42);
```

**21. Invoking a Method Using Reflection**

```csharp
// Invoke the method using reflection.
Type type = typeof(MyType);
MethodInfo method = type.GetMethod("MyMethod");
object returnValue = method.Invoke(myObject, new object[] { });

// Display the return value.
Console.WriteLine(returnValue);
```

**22. Getting the Parameters of a Method Using Reflection**

```csharp
// Get the parameters of the method using reflection.
Type type = typeof(MyType);
MethodInfo method = type.GetMethod("MyMethod");
ParameterInfo[] parameters = method.GetParameters();

// Display the parameters.
foreach (ParameterInfo parameter in parameters)
{
    Console.WriteLine(parameter.Name);
}
```

**23. Getting the Attributes of a Member Using Reflection**

```csharp
// Get the attributes of the member using reflection.
Type type = typeof(MyType);
MemberInfo member = type.GetMember("MyProperty")[0];
Attribute[] attributes = member.GetCustomAttributes(typeof(MyAttribute), false);

// Display the attributes.
foreach (Attribute attribute in attributes)
{
    Console.WriteLine(attribute.ToString());
}
```

**24. Creating a Custom MemberInfo Using Reflection**

```csharp
// Create a custom MemberInfo using reflection.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
typeBuilder.AddMember(new MyMemberInfo("MyProperty", MemberTypes.Property));
```

**25. Retrieving Member Information Using Reflection Emit**

```csharp
// Get the type's member information using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
MemberInfo[] members = typeBuilder.GetMembers(BindingFlags.Public | BindingFlags.Instance);

// Display the member information.
foreach (MemberInfo member in members)
{
    Console.WriteLine(member.Name);
}
```

**26. Getting the Type of a Member Using Reflection Emit**

```csharp
// Get the type of the member using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
MemberInfo member = typeBuilder.GetMember("MyProperty")[0];
Type memberType = member.MemberType;

// Display the member type.
Console.WriteLine(memberType);
```

**27. Checking if a Member is Public Using Reflection Emit**

```csharp
// Check if the member is public using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
MemberInfo member = typeBuilder.GetMember("MyProperty")[0];
bool isPublic = member.IsPublic;

// Display whether the member is public.
Console.WriteLine(isPublic);
```

**28. Getting the Name of a Member Using Reflection Emit**

```csharp
// Get the name of the member using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
MemberInfo member = typeBuilder.GetMember("MyProperty")[0];
string memberName = member.Name;

// Display the member name.
Console.WriteLine(memberName);
```

**29. Getting the Value of a Field Using Reflection Emit**

```csharp
// Get the value of the field using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
FieldInfo field = typeBuilder.GetField("MyField");
object fieldValue = field.GetValue(myObject);

// Display the field value.
Console.WriteLine(fieldValue);
```

**30. Setting the Value of a Field Using Reflection Emit**

```csharp
// Set the value of the field using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
FieldInfo field = typeBuilder.GetField("MyField");
field.SetValue(myObject, 42);
```

**31. Getting the Value of a Property Using Reflection Emit**

```csharp
// Get the value of the property using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
PropertyInfo property = typeBuilder.GetProperty("MyProperty");
object propertyValue = property.GetValue(myObject);

// Display the property value.
Console.WriteLine(propertyValue);
```

**32. Setting the Value of a Property Using Reflection Emit**

```csharp
// Set the value of the property using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
PropertyInfo property = typeBuilder.GetProperty("MyProperty");
property.SetValue(myObject, 42);
```

**33. Invoking a Method Using Reflection Emit**

```csharp
// Invoke the method using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
MethodInfo method = typeBuilder.GetMethod("MyMethod");
object returnValue = method.Invoke(myObject, new object[] { });

// Display the return value.
Console.WriteLine(returnValue);
```

**34. Getting the Parameters of a Method Using Reflection Emit**

```csharp
// Get the parameters of the method using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
MethodInfo method = typeBuilder.GetMethod("MyMethod");
ParameterInfo[] parameters = method.GetParameters();

// Display the parameters.
foreach (ParameterInfo parameter in parameters)
{
    Console.WriteLine(parameter.Name);
}
```

**35. Getting the Attributes of a Member Using Reflection Emit**

```csharp
// Get the attributes of the member using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
MemberInfo member = typeBuilder.GetMember("MyProperty")[0];
Attribute[] attributes = member.GetCustomAttributes(typeof(MyAttribute), false);

// Display the attributes.
foreach (Attribute attribute in attributes)
{
    Console.WriteLine(attribute.ToString());
}
```

**36. Creating a Custom MemberInfo Using Reflection Emit**

```csharp
// Create a custom MemberInfo using reflection emit.
TypeBuilder typeBuilder = TypeBuilder.CreateType("MyType");
typeBuilder.AddMember(new MyMemberInfo("MyProperty", MemberTypes.Property));
```

**37. Retrieving Member Information Using Roslyn**

```csharp
// Get the type's member information using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's members.
MemberDeclarationSyntax[] members = typeDeclaration.Members.ToArray();

// Display the member information.
foreach (MemberDeclarationSyntax member in members)
{
    Console.WriteLine(member.Identifier.Text);
}
```

**38. Getting the Type of a Member Using Roslyn**

```csharp
// Get the type of the member using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's members.
MemberDeclarationSyntax[] members = typeDeclaration.Members.ToArray();

// Get the type of the first member.
TypeSyntax memberType = members[0].Type;

// Display the member type.
Console.WriteLine(memberType);
```

**39. Checking if a Member is Public Using Roslyn**

```csharp
// Check if the member is public using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's members.
MemberDeclarationSyntax[] members = typeDeclaration.Members.ToArray();

// Check if the first member is public.
bool isPublic = members[0].Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.PublicKeyword));

// Display whether the member is public.
Console.WriteLine(isPublic);
```

**40. Getting the Name of a Member Using Roslyn**

```csharp
// Get the name of the member using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's members.
MemberDeclarationSyntax[] members = typeDeclaration.Members.ToArray();

// Get the name of the first member.
string memberName = members[0].Identifier.Text;

// Display the member name.
Console.WriteLine(memberName);
```

**41. Getting the Value of a Field Using Roslyn**

```csharp
// Get the value of the field using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's fields.
FieldDeclarationSyntax[] fields = typeDeclaration.Members.OfType<FieldDeclarationSyntax>().ToArray();

// Get the value of the first field.
ExpressionSyntax fieldValue = fields[0].Declaration.Variables[0].Initializer.Value;

// Display the field value.
Console.WriteLine(fieldValue);
```

**42. Setting the Value of a Field Using Roslyn**

```csharp
// Set the value of the field using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's fields.
FieldDeclarationSyntax[] fields = typeDeclaration.Members.OfType<FieldDeclarationSyntax>().ToArray();

// Set the value of the first field.
fields[0].Declaration.Variables[0].Initializer = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, fields[0].Declaration.Variables[0].Identifier, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(42)));

// Display the new value of the field.
Console.WriteLine(fields[0].Declaration.Variables[0].Initializer.Value);
```

**43. Getting the Value of a Property Using Roslyn**

```csharp
// Get the value of the property using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's properties.
PropertyDeclarationSyntax[] properties = typeDeclaration.Members.OfType<PropertyDeclarationSyntax>().ToArray();

// Get the value of the first property.
ExpressionSyntax propertyValue = properties[0].ExpressionBody.Expression;

// Display the property value.
Console.WriteLine(propertyValue);
```

**44. Setting the Value of a Property Using Roslyn**

```csharp
// Set the value of the property using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's properties.
PropertyDeclarationSyntax[] properties = typeDeclaration.Members.OfType<PropertyDeclarationSyntax>().ToArray();

// Set the value of the first property.
properties[0].ExpressionBody = SyntaxFactory.ExpressionBody(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, properties[0].ExpressionBody.Expression, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(42))));

// Display the new value of the property.
Console.WriteLine(properties[0].ExpressionBody.Expression);
```

**45. Invoking a Method Using Roslyn**

```csharp
// Invoke the method using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's methods.
MethodDeclarationSyntax[] methods = typeDeclaration.Members.OfType<MethodDeclarationSyntax>().ToArray();

// Invoke the first method.
InvocationExpressionSyntax invocation = SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(typeDeclaration.Identifier.Text), methods[0].Identifier));

// Display the result of the invocation.
Console.WriteLine(semanticModel.GetSymbolInfo(invocation).Symbol.ToString());
```

**46. Getting the Parameters of a Method Using Roslyn**

```csharp
// Get the parameters of the method using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's methods.
MethodDeclarationSyntax[] methods = typeDeclaration.Members.OfType<MethodDeclarationSyntax>().ToArray();

// Get the parameters of the first method.
ParameterSyntax[] parameters = methods[0].ParameterList.Parameters.ToArray();

// Display the parameters.
foreach (ParameterSyntax parameter in parameters)
{
    Console.WriteLine(parameter.Identifier.Text);
}
```

**47. Getting the Attributes of a Member Using Roslyn**

```csharp
// Get the attributes of the member using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Get the type's members.
MemberDeclarationSyntax[] members = typeDeclaration.Members.ToArray();

// Get the attributes of the first member.
AttributeListSyntax[] attributes = members[0].AttributeLists.ToArray();

// Display the attributes.
foreach (AttributeListSyntax attributeList in attributes)
{
    Console.WriteLine(attributeList.ToString());
}
```

**48. Creating a Custom MemberInfo Using Roslyn**

```csharp
// Create a custom MemberInfo using Roslyn.
SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(source);
SemanticModel semanticModel = Compilation.Create("MyAssembly").GetSemanticModel(syntaxTree);

// Get the type declaration.
TypeDeclarationSyntax typeDeclaration = syntaxTree.GetRoot().DescendantNodes().OfType<TypeDeclarationSyntax>().First();

// Add a custom member to the type.
MemberDeclarationSyntax customMember = SyntaxFactory.FieldDeclaration(SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("MyCustomMember"))
    .WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier("myCustomMember"))
        .WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(42)))))));

// Add the custom member to the type declaration.
typeDeclaration = typeDeclaration.AddMembers(customMember);

// Display the custom member.
Console.WriteLine(customMember.ToString());
```

**49. Retrieving Member Information Using IL**

```csharp
// Get the type's member information using IL.
Assembly assembly = Assembly.Load("MyAssembly");
Type type = assembly.GetType("MyType");

// Get the type's methods.
MethodInfo[] methods = type.GetMethods();

// Display the member information.
foreach (MethodInfo method in methods)
{
    Console.WriteLine(method.Name);
}
```

**50. Getting the Type of a Member Using IL**

```csharp
// Get the type of the member using IL.
Assembly assembly = Assembly.Load("MyAssembly");
Type type = assembly.GetType("MyType");

// Get the type's methods.
MethodInfo[] methods = type.GetMethods();

// Get the type of the first method.
Type methodType = methods[0].ReturnType

```
