I know I can block a number from messaging/calling me, but I can only do this after the event. Is there a way I can stop unknown numbers from messaging/calling before the event, i.e. for numbers that aren't in my Contacts list? My iPhone is running iOS 11.2.6.
Yes, albeit with some trade offs. See this link for some information. If you go to Settings, hit General > Do Not Disturb > Allow Calls From and select Contacts Only. This setting applies to SMS messages as well. Hopefully this is useful.
You can create a contacts group and add all (or desired) contacts to that group.
On your iPhone, enable Do Not Disturb. Go to Settings app → Do Not Disturb and select
the group All Contacts from within Allow Calls From section under Do Not Disturb.
You can also create a Contacts group to selectively segregate contacts. To create a contacts group, refer to the Apple Support article, iCloud: Create a group and add contacts to it.
Update: Apparently an easier approach is to select All Contacts in the aforementioned step, instead of creating a group and selecting it. All Contacts option is available by default.