How to set the value of an FProperty for an enum type created in a blueprint in C++?

I’ve been working with some plugins lately, which include several blueprint actor classes. These blueprint classes contain various member variables, including floats, enums, and other types. I’ve been able to iterate through these member variables in the blueprint classes using a TFieldIterator iterator and successfully assign values to types like floats and booleans, which are directly accessible in the code.
However, some of these member variables are enums created in the blueprints. I’ve tried to retrieve the names and corresponding values of these enums by casting them using UByteProperty* ByteProperty = Cast(Property); and then accessing the enums through the Enum member of UByteProperty. But how can I assign a value to an FProperty of this enum type? Here’s my code:

		for (TFieldIterator<FProperty> PropertyIt(pUDSClass); PropertyIt; ++PropertyIt)
		{
			FProperty* Property = *PropertyIt;
			if (Property->HasAnyPropertyFlags(CPF_Edit | CPF_BlueprintVisible))
			{
				FString strClassName = Property->GetClass()->GetName();

				if (strClassName.Contains(TEXT("ByteProperty")))
				{
					UByteProperty* ByteProperty = Cast<UByteProperty>(Property);
					if (ByteProperty != nullptr)
					{
						UEnum* EnumType = ByteProperty->Enum;
						if (EnumType != nullptr)
						{
							for (int32 Index = 0; Index < EnumType->NumEnums(); ++Index)
							{
								FString EnumName = EnumType->GetNameStringByIndex(Index);
								int64 EnumValue = EnumType->GetValueByIndex(Index);
							}
						}
					}
				}
			}
		}

First of all, if you’re past 4.26 it should be all FProperty and no UProperty.

Regardless of whether there’s an associated enum or not, byte properties store byte values internally so you can dereference the pointer as an uint8 and set its value directly :

uint8* ValuePtr = ByteProperty->ContainerPtrToValuePtr<uint8>(Container);
*ValuePtr = Value;

Or, depending on what you’re used to do, you can also use one of the helper methods.
Byte should work the same as FIntProperty :

ByteProperty->SetIntPropertyValue(Value);

When in doubt, you can also use the text conversion function and let property internals take care of converting properly. With text import you can specify either numeric value (enum index) or string value (enum value name). This is obviously ambiguous if enum names are digits. Also converting from text is suboptimal.

// by index
ByteProperty->ImportText_InContainer(TEXT("4"), Container, nullptr, PPF_None);

// by name
ByteProperty->ImportText_InContainer(TEXT("ValueNumber4"), Container, nullptr, PPF_None);

When attempting to retrieve the Blueprint enum type used by the ByteProperty type, I managed to successfully obtain a pointer of type UEnum. However, the program crashed when calling UEnum::NumEnums(). The UEnum obtained through casting seems to have many members that are null. Is there a way to obtain the names and corresponding values of these Blueprint enum types in C++?

Here is my Code:

UByteProperty* ByteProperty = Property->ContainerPtrToValuePtr<UByteProperty>(pUDS);
if (ByteProperty != nullptr)
{
	UEnum* EnumType = ByteProperty->Enum;
	if (EnumType != nullptr)
	{
		TArray< TPair <uint8, FString>> arrEnum;
                                      //Crush On here:⬇
		for (int32 Index = 0; Index < EnumType->NumEnums(); ++Index)
		{
			TPair <uint8, FString> PairEnum;
			PairEnum.Key = EnumType->GetValueByIndex(Index);
			PairEnum.Value = EnumType->GetNameStringByIndex(Index);
			arrEnum.Add(PairEnum);
		}
     }
}