4. As shown in Figure 1, in trapezoid $A B C D$, $A B / / C D$, $A B=a, C D=b$. If $\angle A D C=\angle B F E$, and the area of quadrilateral $A B F E$ is equal to the area of quadrilateral $C D E F$, then the length of $E F$ is ( ).
(A) $\frac{a+b}{2}$
(B) $\sqrt{a b}$
(C) $\frac{2 a b}{a+b}$
(D) $\sqrt{\frac{a^{2}+b^{2}}{2}}$
4.D.

As shown in Figure 3, extend $DA$ and $CB$ to intersect at point $P$.
From the given conditions, we have
$$
\begin{array}{l}
\angle PAB = \angle ADC \\
= \angle BFE.
\end{array}
$$

Therefore,
$\triangle PAB \backsim \triangle PFE$
$\backsim \triangle PDC$.
Let $EF = x, S_{\triangle PAB} = S_0, S_{\text{quadrilateral } ABFE} =$
$S_{\text{quadrilateral CDEF}} = S$.
$$
\begin{array}{l}
\text{Then } \frac{S_0 + S}{S_0} = \left(\frac{EF}{AB}\right)^2 = \frac{x^2}{a^2} \\
\Rightarrow \frac{S}{S_0} = \frac{x^2 - a^2}{a^2}, \\
\frac{S_0 + 2S}{S_0} = \left(\frac{CD}{AB}\right)^2 = \frac{b^2}{a^2} \\
\Rightarrow \frac{2S}{S_0} = \frac{b^2 - a^2}{a^2}.
\end{array}
$$

From equations (1) and (2), we get $EF = x = \sqrt{\frac{a^2 + b^2}{2}}$.